2009-05-22 11 views
6

Sto scherzando cercando di imparare i dettagli di LINQ. Voglio convertire la seguente query (che funziona correttamente) dalla sintassi della query alla sintassi del metodo, ma non riesco a farlo bene. Qualcuno può mostrarmi il modo corretto per farlo?Come convertire la sintassi della query nella sintassi del metodo

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

Per info, i capitoli successivi a "C# in profondità" coprono questo settore in grande dettaglio . –

+0

La specifica C# 3.0 è disponibile su Internet; descrive le regole di trasformazione sintattica in grande dettaglio. –

risposta

14

In realtà è piuttosto semplice;

from entry in A 
where B 

traduce (letteralmente) a:

A.Where(entry=>B) 

e:

select C 

traduce direttamente a (con "la voce" come il nostro contesto):

.Select(entry=>C) 

(tranne quando sarebbe.210, che il compilatore omette per i casi non banali)

quindi basta iniettare quelli e il gioco è fatto:

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

Bello - grazie per la risposta rapida. –

+0

+1 per una risposta bella e pulita che spiega il principio piuttosto che una semplice soluzione all'esempio specifico. – BitMask777

+1

Bart De Smet ha scritto una comoda traduzione [cheat sheet] (http://bartdesmet.net/blogs/bart/archive/2008/08/30/c-3-0-query-expression-translation-cheat-sheet.aspx) per passare tra il metodo e la sintassi della query. – oillio

Problemi correlati