0

我们如何向 linq 子选择查询添加 where 条件。

IE

List<CallLog> callLog = CallLog.SampleData();
List<Contacts> contacts = Contacts.SampleData();


var q = from call in callLog
        where call.Incoming == true
        group call by call.Number into g
        select new contacts { 
                              contact.FirstName = g.FirstName, 
                              contact.LastName = g.LastName, 
                             Count = g.Count(), 
                             Avg = g.Average( c => c.Duration ) <--- WHERE c.Duration > 5, 
                             Total = g.Sum( c => c.Duration )   <--- WHERE c.Duration >= 60
                            };

如上所示,我们如何在 LINQ 语句中添加“Where 条件”?

4

2 回答 2

0

只需Where使用您描述的子句添加一个方法!

Avg   = g.Where(c => c.Duration > 5).Average(c => c.Duration), 
Total = g.Where(c => c.Duration >= 60).Sum(c => c.Duration)
于 2010-01-22T01:17:56.997 回答
0

你几乎拥有它:

var q = from call in callLog
        where call.Incoming == true
        group call by call.Number into g
        select new contacts { 
                contact.FirstName = g.FirstName, 
                contact.LastName = g.LastName, 
                            Count = g.Count(), 
                            Avg   = g.Where(c => c.Duration > 5).Average( c => c.Duration ),
                            Total = g.Where(c => c.Duration >= 60).Sum( c => c.Duration )
            };
于 2010-01-22T01:18:09.753 回答