Linq SQL GroupJoin错误

时间:2017-02-10 12:49:19

标签: c# linq linq-to-sql

这是我的代码显示错误,如

  

严重级代码描述项目文件行抑制状态   错误CS0411无法根据用法推断方法Queryable.GroupJoin<TOuter, TInner, TKey, TResult>(IQueryable<TOuter>, IEnumerable<TInner>, Expression<Func<TOuter, TKey>>, Expression<Func<TInner, TKey>>, Expression<Func<TOuter, IEnumerable<TInner>, TResult>>)的类型参数。尝试明确指定类型参数

data.RawMaterail.Where(c => c.Category.categoryType == 1)
                .Join(data.Sizes, x => x.DiamondSize.diamondSizeId, 
                                  y => y.DiamondSize.diamondSizeId, 
                                  (x, y) => new { RM = x, Size = y })
                .GroupJoin(data.PriceLevels.Where(c => c.priceLevelId == PriceLevelId), 
                           x => new { x.RM.rMId , x.Size.sizeId}, 
                           y => new { y.rmId , y.sizeId}, 
                           (y, x) => new { Category = y, PurityLevel = x })
                .SelectMany(xy => xy.PurityLevel.DefaultIfEmpty(), 
                           (x, y) => new { Category = x.Category, PurityLevel = y })
                .Select(item => new
                 {
                     Code = item.Category.RM.rMCode + " " + item.Category.Size.sizeName,
                     Name = item.Category.RM.rMName + " " + item.Category.Size.sizeName,
                     Date = item.PurityLevel.rowDate,
                     Id = (int)item.Category.RM.rMId,
                     RateId = (int?)item.PurityLevel.stonePriceLevelId ?? 0,
                     Price = (double?)item.PurityLevel.price ?? 0,
                     PriceLevelId = (int?)item.PurityLevel.priceLevelId ?? 0,
                     TypeId = (int)item.Category.Size.sizeId,
                     IsRateChanged = false
                 }).OrderBy(c => c.Date).ThenBy(n => n.TypeId).ToList();

基于GroupJoin位置的错误显示以及如何加入此类型的左连接

2 个答案:

答案 0 :(得分:1)

尝试将键选择器更改为

x => new { x.RM.rMId , x.Size.sizeId}, 
y => new { y.rmId , y.sizeId}, 

x => new { RMId = x.RM.rMId , SizeId = x.Size.sizeId}, 
y => new { RMId = y.rmId, SizeId = y.sizeId}, 

其中一个属性名为rMId,另一个rmIdm的大小写不同)。因此编译器会生成两种不同的匿名类型。但TKey需要与外键和内键选择器相同类型

通过明确声明匿名类型的属性名称可以避免此错误,并且编译器对两个键选择器使用相同的匿名类型。

答案 1 :(得分:1)

data.RawMaterail.Where(c => c.Category.categoryType == 1)
                .Join(data.Sizes, x => x.DiamondSize.diamondSizeId, 
                                  y => y.DiamondSize.diamondSizeId, 
                                  (x, y) => new { RM = x, Size = y })
                .GroupJoin(data.PriceLevels.Where(c => c.priceLevelId == PriceLevelId), 
                           x => new { RID = (int?)x.RM.rMId , SID = (int?)x.Size.sizeId}, 
                           y => new { RID = (int?)y.rmId , SID = (int?)y.sizeId}, 
                           (y, x) => new { Category = y, PurityLevel = x })
                .SelectMany(xy => xy.PurityLevel.DefaultIfEmpty(), 
                           (x, y) => new { Category = x.Category, PurityLevel = y })
                .Select(item => new
                 {
                     Code = item.Category.RM.rMCode + " " + item.Category.Size.sizeName,
                     Name = item.Category.RM.rMName + " " + item.Category.Size.sizeName,
                     Date = item.PurityLevel.rowDate,
                     Id = (int)item.Category.RM.rMId,
                     RateId = (int?)item.PurityLevel.stonePriceLevelId ?? 0,
                     Price = (double?)item.PurityLevel.price ?? 0,
                     PriceLevelId = (int?)item.PurityLevel.priceLevelId ?? 0,
                     TypeId = (int)item.Category.Size.sizeId,
                     IsRateChanged = false
                 }).OrderBy(c => c.Date).ThenBy(n => n.TypeId).ToList();