首页 > 解决方案 > C# Linq 使用 where 子句压缩连接查询

问题描述

嗨,我正在使用下面的代码使用 linq 语法从 2 个表中获取所需的数据,这工作正常。

var ratings = from r in _ratingRepository.AsQueryable()
              join c in _convRepository.AsQueryable()
              on r.SessionId equals c.CurrentConversationSid
              where!c.IsDeleted && c.DateCreated >= request.From && c.DateCreated <= 
              request.To && c.HasRated
              select new Rating() {
               Id = r.Id,
               SessionId = r.SessionId,
               Questions = r.Questions,
               AvgRatingValue = r.AvgRatingValue
            };

我想使用以下语法转换此代码

IQueryable<Rating> ratingsObj = _ratingRepository.AsQueryable()
                .Join(_convRepository.AsQueryable().Where(a => a.HasRated), r => r.SessionId, c => c.CurrentConversationSid, (r, c) =>
                    new Rating()
                    {
                        Id = r.Id,
                        SessionId = r.SessionId,
                        Questions = r.Questions,
                        AvgRatingValue = r.AvgRatingValue
                    });

它给出以下错误

System.ArgumentException:' 1[Flecx.Chat.Entities.Conversation]' cannot be used for parameter of type 'System.Linq.IQueryableSystem.Linq.IQueryable 1[Flecx.Chat.Entities.Conversation] 方法的'System.Collections.Generic.IEnumerable 1[Flecx.Chat.Entities.Conversation]'类型的表达式1[Flecx.Chat.Entities.Conversation] Where[Conversation](System.Linq.IQueryable,System.Linq。 Expressions.Expression 1[System.Func2[Flecx.Chat.Entities.Conversation,System.Boolean]])'(参数'arg0')'

如果我删除此代码 .Where(a => a.HasRated) 它运行良好。如何在上述语法中包含 where 子句。

需要帮忙

标签: c#linq

解决方案


尝试这个:

 var ratingsObj = _ratingRepository.AsQueryable()
                            .Join(_convRepository.AsQueryable(), 
                                  r => r.SessionId, 
                                  c => c.CurrentConversationSid,
                                  (r,c)=>new {r,c})  //**
                            .Where(a => a.c.HasRated)
                            .Select(x => new Rating()
                            {
                                Id = x.r.Id,
                                SessionId = x.r.SessionId,
                                Questions = x.r.Questions,
                                AvgRatingValue = x.r.AvgRatingValue
                            });

您可以使用下面的“//**”过滤任何您想要的内容:

(r, c) => new 
            { r.Id,
              r.SessionId,
              r.Questions,
              r.AvgRatingValue,
              c.HasRated
            }

然后您的代码更改为:

 var ratingsObj = _ratingRepository.AsQueryable()
                          .Join(_convRepository.AsQueryable(),
                                r => r.SessionId,
                                c => c.CurrentConversationSid,
                                (r, c) => new
                                { r.Id,
                                r.SessionId,
                                r.Questions,
                                r.AvgRatingValue,
                                c.HasRated})
                          .Where(a => a.HasRated)
                          .Select(x => new Rating()
                          {
                              Id = x.Id,
                              SessionId = x.SessionId,
                              Questions = x.Questions,
                              AvgRatingValue = x.AvgRatingValue
                          });

推荐阅读