2009-05-22 29 views
6

Tôi đang lừa xung quanh cố gắng tìm hiểu thêm về LINQ. Tôi muốn chuyển đổi các truy vấn sau đây (mà đang làm việc một cách chính xác) từ cú pháp truy vấn cú pháp phương pháp, nhưng tôi dường như không thể làm cho nó đúng. Bất cứ ai có thể chỉ cho tôi cách chính xác để thực hiện điều đó?Cách chuyển đổi cú pháp truy vấn sang cú pháp phương pháp

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) 
       }; 
+1

Đối với thông tin, các chương sau này trong "C# in x" bao gồm khu vực này một cách chi tiết rực rỡ . –

+0

Đặc điểm kỹ thuật C# 3.0 có sẵn trên internet; nó mô tả các quy tắc chuyển đổi cú pháp một cách chi tiết. –

Trả lời

14

Nó thực sự khá đơn giản;

from entry in A 
where B 

dịch (theo nghĩa đen) tới:

A.Where(entry=>B) 

và:

select C 

dịch trực tiếp (với "entry" như bối cảnh của chúng tôi):

.Select(entry=>C) 

(trừ khi nó sẽ là.210, trong đó trình biên dịch bỏ qua đối với trường hợp không tầm thường)

vì vậy chỉ cần tiêm những và bạn đã hoàn tất:

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, 
         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) 
        }); 
+0

Nice - cảm ơn câu trả lời nhanh. –

+0

+1 cho câu trả lời hay, rõ ràng giải thích nguyên tắc thay vì chỉ là giải pháp cho ví dụ cụ thể. – BitMask777

+1

Bart De Smet đã viết một bản dịch thuận tiện [cheat sheet] (http://bartdesmet.net/blogs/bart/archive/2008/08/30/c-3-0-query-expression-translation-cheat-sheet.aspx) để đi giữa cú pháp phương thức và truy vấn. – oillio

Các vấn đề liên quan