将2个LINQ组合成一个调用

时间:2010-10-19 16:40:59

标签: c# linq linq-to-sql

我使用2个类似的LINQ查询来返回结果,唯一的区别是where子句(&& s.OptIn == "Yes")。有没有办法只用一个查询来执行它?

而不是

的结果
A   2 
B   3

的另一个结果
A 1
B 1

我想要

A   2   1 
B   3   1

这是LINQ:

        var result = from s in pdc.ScanLogs
                     from e in pdc.Exhibits
                     from ce in pdc.ClientEvents
                     where s.ExhibitID == e.ExhibitID
                     && e.ClientEventID == ce.ClientEventID
                     group 1 by new { ce.EventID } into d
                     select new {
                         EventID = d.Key.EventID,
                         Count = d.Count()
                     };

        var result = from s in pdc.ScanLogs
                     from e in pdc.Exhibits
                     from ce in pdc.ClientEvents
                     where s.ExhibitID == e.ExhibitID
                     && e.ClientEventID == ce.ClientEventID
                     && s.OptIn == "Yes"
                     group 1 by new { ce.EventID } into d
                     select new {
                         EventID = d.Key.EventID,
                         Count = d.Count()
                     };

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

IQueryable<ScanLog> scanlogs = pdc.ScanLogs;
if (filter) scanlogs = scanlogs.Where(...);
var result = from s in scanlogs
   ...