LINQ where条件的子查询

怎样才能增加一个WHERE条件到LINQ子查询的查询。

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 };

我们怎样才能加一个“Where条件”的LINQ语句如上所示?

--------------解决方案-------------

只需添加一Where跟你描述的从句方法!

Avg = g.Where(c => c.Duration > 5).Average(c => c.Duration),
Total = g.Where(c => c.Duration >= 60).Sum(c => c.Duration)

你几乎拥有了:

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

分类:LINQ 时间:2015-03-15 人气:0
本文关键词: LINQ,条件,其中,
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.642 (s). 10 q(s)