使用join在LINQ中实现where子句

时间:2015-11-17 15:50:27

标签: c# linq

我有这个LINQ(LINQ TO ENTITY):

var clientFullReview = (from cr in clientReview
                        join ir in inspectionReviews on cr.siteId equals ir.SiteId into g
                        from subsite in g.DefaultIfEmpty()
                        select new
                        {
                         clientId = cr.clientId,
                         clientName = cr.clientName,
                         siteId = cr.siteId == null ? -1 : cr.siteId,
                         inspectionReviewId = (subsite == null ? -1 : subsite.Id),
                         inspectionFrequency = (subsite == null ? -1 : subsite.FrequencyId),

                          isNormal = (subsite == null ? false : subsite.IsNormal)
                         });

在上面的链接中,我需要使用where子句。

inspectionReviews - 具有日期属性。 在LINQ abouve中,我想仅连接具有特定日期的inspectionReviews记录,如:

    var clientFullReview = (from cr in clientReview
                            join ir in inspectionReviews on cr.siteId equals
                            where ir.DateReview.Year == date.Year &&
                            ir.DateReview.Month == date.Month 
                            into g
                            from subsite in g.DefaultIfEmpty()
                            select new
                            {
                             clientId = cr.clientId,
                             clientName = cr.clientName,
                             siteId = cr.siteId == null ? -1 : cr.siteId,
                             inspectionReviewId = (subsite == null ? -1 : subsite.Id),
                             inspectionFrequency = (subsite == null ? -1 : subsite.FrequencyId),

                              isNormal = (subsite == null ? false : subsite.IsNormal)
                             });

但是当我尝试用where子句实现它时,我得到了这个错误:

A query body must end with a select clause or a group clause

关于这个关键词:into在第二个LINQ。

所以我的问题是如何在实施连接时按日期过滤数据?

3 个答案:

答案 0 :(得分:1)

单程

join ir in inspectionReviews.Where(x =>
    x.DateReview.Year == date.Year && x.DateReview.Month == date.Month)
on cr.siteId equals ir.SiteId

另一种方式

join ir in (from x in inspectionReviews
    where x.DateReview.Year == date.Year && x.DateReview.Month == date.Month
    select x)
on cr.siteId equals ir.SiteId

又一种方式

join ir on cr.siteId equals ir.SiteId into g
from subsite in g
    .Where(x => x.DateReview.Year == date.Year && x.DateReview.Month == date.Month)
    .DefaultIfEmpty()

答案 1 :(得分:1)

Join子句只允许等于,所以如果你需要过滤连接的集合,你可以使用第二个from子句下的子网站变量:

<% if @category %>

    <% else %>

    <% end %>

答案 2 :(得分:1)

将其分解为两个查询,并从filterest第一个列表中进行第二次选择。

我不打算重现你的代码,因为我认为它在第二个连接值上缺少一些文本,但我们的想法是分两步完成:

 var clientFullReviews = (from cr in clientReview
                        join ir in inspectionReviews on cr.siteId equals
                        where ir.DateReview.Year == date.Year &&
                        ir.DateReview.Month == date.Month 
                        into g

 var clientcurrent reviews =(from cr clientFullReviews select new
                        {
                         clientId = cr.clientId,
                         clientName = cr.clientName,
                         siteId = cr.siteId == null ? -1 : cr.siteId,
                         inspectionReviewId = (subsite == null ? -1 : subsite.Id),
                         inspectionFrequency = (subsite == null ? -1 : subsite.FrequencyId),

                          isNormal = (subsite == null ? false : subsite.IsNormal)
                         });

这不是完美的语法,因为我不太了解您的数据对象,但您明白了。我不确定你是否会以这种方式进行性能测试,但我几乎总是这样打破它以保持我的Linq语法干净和可读(并且避免在一行中混淆太多扩展表达式!)