【问题标题】:Implementing where clause in LINQ with join在 LINQ 中使用 join 实现 where 子句
【发布时间】:2016-02-19 01:53:08
【问题描述】:

我有这个 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 中,我只想加入具有特定日期的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

关于这个关键字:第二个 LINQ 中的 into

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

【问题讨论】:

    标签: c# linq


    【解决方案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()
    

    【讨论】:

      【解决方案2】:

      join子句只允许equals,所以如果需要过滤joined集合,可以使用第二个from子句下的subsite变量:

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

      【讨论】:

        【解决方案3】:

        将其分成两个查询,然后从最过滤的第一个列表中进行第二次选择。

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

         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 语法简洁易读(并避免在一行中被太多扩展表达式混淆!)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-29
          • 1970-01-01
          • 1970-01-01
          • 2010-11-03
          • 1970-01-01
          相关资源
          最近更新 更多