我正在努力学习LINQ的输出.我想将以下查询(这是正常工作)从查询语法转换为方法语法,但我似乎无法正确.任何人都可以告诉我正确的方法吗?
var logQuery = from entry in xDoc.Descendants("logentry") where (entry.Element("author").Value.ToLower().Contains(matchText) || entry.Element("msg").Value.ToLower().Contains(matchText) || entry.Element("paths").Value.ToLower().Contains(matchText) || entry.Element("revision").Value.ToLower().Contains(matchText)) select new { Revision = entry.Attribute("revision").Value,Author = entry.Element("author").Value,CR = LogFormatter.FormatCR(entry.Element("msg").Value),Date = LogFormatter.FormatDate(entry.Element("date").Value),Message = LogFormatter.FormatComment(entry.Element("msg").Value),ET = LogFormatter.FormatET(entry.Element("msg").Value),MergeFrom = LogFormatter.FormatMergeFrom(entry.Element("msg").Value),Mergeto = LogFormatter.FormatMergeto(entry.Element("msg").Value) };
解决方法
它实际上非常简单;
from entry in A where B
将(字面意思)翻译为:
A.Where(entry=>B)
和:
select C
直接翻译(以“条目”作为我们的上下文):
.Select(entry=>C)
(当它是entry =>条目时,编译器忽略了非重要的情况除外)
所以只需注入那些就完成了:
var logQuery = xDoc.Descendants("logentry") .Where(entry=> entry.Element("author").Value.ToLower().Contains(matchText) || entry.Element("msg").Value.ToLower().Contains(matchText) || entry.Element("paths").Value.ToLower().Contains(matchText) || entry.Element("revision").Value.ToLower().Contains(matchText)) .Select(entry=>new { Revision = entry.Attribute("revision").Value,Mergeto = LogFormatter.FormatMergeto(entry.Element("msg").Value) });
原文地址:https://www.jb51.cc/csharp/98968.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。