【问题标题】:LINQ: Get the Parent object properties and a single child property as a flat structureLINQ:获取父对象属性和单个子属性作为平面结构
【发布时间】:2015-01-02 00:47:48
【问题描述】:

我有一个包含在名为 Branch 的父对象中的地址列表。分支可能有也可能没有定义这些地址,我需要得到一个平面层次结构或这些分支和地址。

var x = from p in CurrentBranchList 
        where p.ScheduledForDeletion == false
        from c in p.Addresses 
        where c.ScheduledForDeletion == false && c.AddressTypeId == 3
        select new
        {
          BranchId = p.BranchId,
          Name = p.Name,
          Address = (c == null) ? "" : c.Address1 + " " + c.Address2,
          City = (c == null) ? "" : c.City,
          State = (c == null) ? 0 : c.StateId
        };

以上是我尝试过的,但是如果缺少地址,我将无法获得有关分支的信息...我仍在尝试弄清楚如何使用 Linq 进行此操作。在 SQL 中,我将刚刚加入这两个表以获取该信息。

谁能帮我解决这个问题...我敢肯定这是一件很容易的事情。谢谢。 PS。我知道这与 (Linq query to return a flatened list of parent child) 非常相似,但其中的孩子始终存在。


编辑 - 工作解决方案 以下是似乎对我有用的代码。我无法针对源数据库使用数据库,因为 CurrentBranchList 中包含的对象是在内存中编辑的,并且持久性是在单个操作中执行的。

var x = from p in CurrentBranchList
        join c in CurrentBranchList.SelectMany(b => b.Addresses) 
          on p.EntityId equals c.EntityId into ur 
        where p.ScheduledForDeletion == false      
        from u in ur.DefaultIfEmpty() 
        select new
        {
          BranchId = p.BranchId,
          Name = p.Name,
          Address = (u == null) ? "" : u.Address1 + " " + u.Address2,
          City = (u == null) ? "" : u.City,
          State = (u == null) ? 0 : u.StateId
        };

感谢您的帮助。这些链接确实帮助我理解了需要发生的事情。

我还尝试了 Daniel Brückner 的解决方案,它看起来更优雅并且需要更少的输入。 :-) 似乎在我尝试的几种情况下都有效。

这就是它的样子。

var xx = CurrentBranchList.SelectMany(b => b.Addresses.DefaultIfEmpty().Select(a => new 
        {
          BranchId = b.BranchId,
          Name = b.Name,
          Address = (a == null) ? "" : a.Address1 + " " + a.Address2,
          City = (a == null) ? "" : a.City,
          State = (a == null) ? 0 : a.StateId
        }));

【问题讨论】:

    标签: c# linq


    【解决方案1】:
    IQueryable<Branch> branches = GetBranches();
    
    var result = braches.
       SelectMany(b => b.Addresses.
          DefaultIfEmpty().
          Select(a => new { Branch = b, Address = a }));
    

    【讨论】:

      【解决方案2】:

      您需要左外连接而不是内连接。 Here's how.

      【讨论】:

        【解决方案3】:

        查看this post,它展示了如何使用 DefaultIfEmtpy 构造来执行 LEFT JOIN。

        恐怕不是 Linq 最容易发现的功能。

        【讨论】:

          【解决方案4】:

          抱歉,也许我来晚了 - 但我遇到了类似的问题,即使在这篇文章发布 5.5 年后也找不到具体的答案。我的解决方案(未优化 - 但有效):

          public class YearDayBill
          {
              public string Name;
              public int YearDay;
          }
          
          public class EatOutDaysOfYear
          {
              public string Name;
              public List<int> YearDays;
          }
          
          public static class Program
          {
              static void Main()
              {
                  var eatouts = new List<EatOutDaysOfYear>
                  {
                      new EatOutDaysOfYear {Name = "amit", YearDays = new List<int>() {59, 37, 31, 17, 29}},
                      new EatOutDaysOfYear {Name = "prakash", YearDays = new List<int>() {6, 18, 13}},
                      new EatOutDaysOfYear {Name = "sarvo", YearDays = new List<int>() {9, 7, 47, 56, 82, 96}},
                      new EatOutDaysOfYear {Name = "akshay", YearDays = new List<int>() {8, 5, 2, 4}}
                  };
          
                  // query to get properties of parent ('Name') and single child element 
                  var bills = eatouts
                      .SelectMany(a => a.YearDays
                          .Select(b => new YearDayBill {Name = a.Name, YearDay = b})) 
                      .OrderBy(d => d.Name)     // optional 
                      .ThenBy(e => e.YearDay)   // optional 
                      .ToList();
          
                  bills.ForEach(a => Console.WriteLine(string.Concat(a.Name, " | ", a.YearDay)));
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-01-25
            • 1970-01-01
            • 2011-05-12
            • 1970-01-01
            • 2017-05-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多