【问题标题】:OrderBy child property linq to sqlOrderBy 子属性 linq to sql
【发布时间】:2018-09-25 15:14:15
【问题描述】:

有人可以帮我在 linq to sql 中订购嵌套集合吗?请参阅下面的示例:

    public class A
    {
        public int Id { get; set; }
    }

    public class B
    {
        public virtual ICollection<A> ListA { get; set; }
    }

从 db 中调用数据:

_unitOfWork.DbContext.B
            .OrderByDescending(ev => ev.ListA.OrderBy(a => a.Id))
            .ToList();

当 ListA 不为空时,一切正常。但是如果列表 A 为空,我会得到一个异常,即至少有一个对象需要实现 IComparable。有没有办法克服这个问题?

【问题讨论】:

  • ListA 何时为空?或者当ListA 为空时?
  • @xdtTransform null
  • 对于像 new[] { new B{ListA = new []{ new A { Id = 3 },new A { Id = 1 },new A { Id = 2 }} } ,new B{ListA = new []{ new A { Id = 3 },new A { Id = 1 }} } ,new B{ListA = new []{ new A { Id = 999 }} } ,new B{ListA = new []{ new A { Id = 3 },new A { Id = 1 },new A { Id = 2 },new A { Id = 4 }} } ,new B{ListA = new List&lt;A&gt;{} } ,new B{ListA = null } }; 这样的 B。预期的结果是什么?按元素数量排序列表?最大限度?总和?

标签: c# entity-framework linq linq-to-sql


【解决方案1】:

您不能按 null 排序。 null 显然没有实现 IComparable。 在 orderby 前添加 where 子句,检查 listA 是否为空。

_unitOfWork.DbContext.B
        .Where(ev => ev.ListA != null)
        .OrderByDescending(ev => ev.ListA.OrderBy(a => a.Id))
        .ToList();

【讨论】:

  • 在这种情况下,我们只是限制加载具有空 ListA 的父实体。但我正在寻找一种加载所有实体的方法,尽管它是空的。但仅对非空实体应用过滤
  • 好吧,您不是在过滤,而是在订购。而且你不能订购空值。
【解决方案2】:

如果目标是让 B 在 ListA 中包含Something,然后是在 Bs 中包含空集合,然后是在 Bs 中包含 Null 引用。
使用同样排序的 ListA,您可以检查 null 和空列表,然后根据它进行排序。

var Bs = new[] {
    new B{ListA = new []{ new A { Id = 3 },new A { Id = 1 },new A { Id = 2 }} }
    ,new B{ListA = new List<A>{} }
    ,new B{ListA = null }
};

var result = Bs.Select(b =>
                {
                    int i = 0;
                    if (b.ListA == null)
                    {
                        i = 2;
                    }
                    else if (!b.ListA.Any())
                    {
                        i = 1;
                    }
                    else {
                        b.ListA = b.ListA.OrderBy(a => a.Id).ToList();
                    }
                    return new { oIndex = i, value = b };
                })
                .OrderByDescending(x => x.oIndex)
                .Select(g => g.value);

您说:“当 ListA 不为空时一切正常”;我不确定如何理解。因为下面的代码将抛出没有任何 null 或空集合。

var Bis = new[] {
            new B{ListA = new []{ new A { Id = 3 },new A { Id = 1 },new A { Id = 2 }} }
            ,new B{ListA = new []{ new A { Id = 3 },new A { Id = 1 }} }
            ,new B{ListA = new []{ new A { Id = 999 }} }
            ,new B{ListA = new []{ new A { Id = 3 },new A { Id = 1 },new A { Id = 2 },new A { Id = 4 }} }
        };
var result = Bis.OrderByDescending(ev => ev.ListA.OrderBy(a => a.Id)).ToList();

如果您正在比较列表,您需要在某些方面比较它们。 {1, 2, 3} 是否大于 {1,1,1,1}? {1,2,3} 与 {999, 999} 相比如何? {2} 和 {1,1}?因为它有更多的元素?更大的价值?如果一个有更大的价值,但其他的总和是两倍大怎么办?对于 2 个 0 列表 .. 您根据总和和值进行比较?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多