【问题标题】:How to order Nested Collections in Linq and EF如何在 Linq 和 EF 中订购嵌套集合
【发布时间】:2014-10-16 05:15:55
【问题描述】:

我想为我的数据创建一个树列表视图。

树应该是这样的

帐户
-> 提供者
-> 帐户

public sealed class AccountRoot    
{
    public AccountRoot()
    {
        Providers = new Collection<Hoster>();
    }

    public long AccountRootId { get; set; }
    public ICollection<Hoster> Providers { get; set; } 

}

public sealed class Hoster
{
    public Hoster()
    {
        Accounts = new Collection<Account>();
    }

    [Key]
    public long HosterId { get; set; }
    public long AccountRootId { get; set; }
    public string Name { get; set; }

    public ICollection<Account> Accounts { get; set; } 
}

public sealed class Account
{
    [Key]
    public long AccountId { get; set; }
    public long HosterId { get; set; }
    public Hoster Hoster { get; set; }
    public string Name { get; set; }
}

我想订购我的查询。

应该是这样的

帐户
供应商 A-Z
帐户 A-Z

到目前为止我得到的是..

var query = _entity.AccountRoot.Local
                   .Select(x => new AccountRoot()
                   {
                        AccountRootId = x.AccountRootId,
                        Providers = x.Providers.OrderBy(y => y.Name).ToList()
                   }).ToList();

缺少的是下一个嵌套集合的orderby

感谢您的帮助! :-)

【问题讨论】:

  • 你为什么选择Local btw?
  • 我的实体是单例的,因为我需要它作为列表...确实尝试过使用短期实体,但没有达到我的目的...谢谢!
  • 您需要一个查询来获得最终结果吗?或者您只需要对预先填充的对象(LocaleObservableCollection)中的数据进行排序即可达到最终结果?

标签: c# linq entity-framework frameworks entity


【解决方案1】:

根据您是否已经有一个结果集,并且只想在代码中对其进行排序,或者您是否想为 EF 构造 IQueryable&lt;&gt; ,这可能会有所不同,这将被成功编译为 SQL 并执行数据库中的实际排序。

首先,假设您已经在代码中拥有该集合。在这种情况下,您有对象AccountRoot,其中包含Providers 的集合,每个对象都有Accounts 的集合。显然,您不能返回相同的对象,因为您需要重新排序集合属性,所以您只需要构造新的对象。我只会对集合进行排序,但如果需要,您可以构建全新的实体:

var query = ...
    .Select(x => new AccountRoot
    {
        // add copy properties here
        // ....
        Providers = x.Providers
                        .Select(y =>
                        {
                            // Here we can construct completely new entity, 
                            // with copying all properties one by one,
                            // or just reorder existing collection as I do here
                            var result = y;
                            result.Accounts = y.Accounts.OrderBy(z => z.Name).ToArray();
                            return result;
                        })
                        .OrderBy(y => y.Name)
                        .ToArray()
    })
    .ToArray();

第二种情况,如果您需要直接从 SQL 获取它,则有点不同,因为您不能在 lambda 中使用所有 var result = ...; ... return result 的东西 - 它不会编译为 SQL。但想法是一样的——你需要从数据集中构造投影。应该是这样的:

var query = ...
    .Select(x => new AccountRoot
    {
        AccountRootId = x.AccountRootId,
        // Other properties to copy
        // ...
        Providers = x.Providers
                        .Select(y => new Hoster
                        {
                            HosterId = y.HosterId,
                            // Other properties to copy
                            // ...
                            Accounts = y.Accounts.OrderBy(z => z.Name).ToArray(),
                        })
                        .OrderBy(y => y.Name)
                        .ToArray()
    })
    .ToArray();

【讨论】:

  • 按我想要的方式工作!!完美的答案...我选择了第二个,因为我直接需要它:-) 谢谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-26
  • 1970-01-01
  • 2019-04-27
相关资源
最近更新 更多