【问题标题】:How to remove error “Cannot compare elements of type 'System.Linq.IGrouping`2”如何删除错误“无法比较 'System.Linq.IGrouping`2 类型的元素”
【发布时间】:2014-02-10 18:32:30
【问题描述】:

vs2012 C# ef 上工作。我的波纹管 linq 语法向我显示错误,如果我添加 AsEnumerable() 那么它可以正常工作,但它会使查询变慢,变慢 意味着很慢。怎么办。如何避免这个错误

var query = (
                    from bm in this.Context.BilBillMasters

                        join g in
                            (
                                from c in this.Context.BilBillDetails
                                group c by new { c.BillID }
                            )
                        on bm.BillID equals (g == null ? 0 : g.Key.BillID) into bDG
                        from billDetailGroup in bDG.DefaultIfEmpty()


                        where bm.IsDeleted == false
                        && (companyID == 0 || bm.CompanyID == companyID)
                        && (userID == 0 || bm.CustomerID == userID)
                        select new
                        {
                            bm.BillID,
                            BillNo = bm.CustomCode,
                            bm.BillDate,
                            BillMonth = bm.MonthFrom,
                            TransactionTypeID = bm.TransactionTypeID ?? 0,
                            CustomerID = bm.CustomerID,
                            Total = billDetailGroup.Sum(p => p.Amount),


                            bm.ReferenceID,
                            bm.ReferenceTypeID

                        }
                        );

                return query.OrderByDescending(e => e.BillID);

错误信息

如果有任何疑问请询问。在此致谢。任何类型的建议都可以接受。

【问题讨论】:

  • 你试过删除g == null吗? EF 查询生成器不应该需要它,而且它可能会造成混淆。
  • Luaan 感谢回复,g中没有null。假设有一组null,使用.AsEnumerable()后如何解决。

标签: c# linq entity-framework


【解决方案1】:

您创建没有比较器的匿名类:

group c by new { c.BillID }

不如试试

group c by c.BillID

【讨论】:

  • bool b = new { a = 1 }.Equals(new { a = 1 });
  • semao,谢谢回复。在使用新语法后,.problem 在 bm.BillID 上剩余加入 g (从 c in this.Context.BilBillDetails group c by c.BillID//new { c.BillID } )等于 (g == null ? 0 : g. ToLong()) 从 bDG.DefaultIfEmpty() 中的 billDetailGroup 到 bDG
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-08
  • 2014-08-19
相关资源
最近更新 更多