LINQ:获取Parent对象属性和单个子属性作为平面结构

时间:2009-04-28 17:52:12

标签: c# linq

我有一个名为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
        };

感谢您的帮助。这些链接确实帮助我了解了需要做些什么。

我也尝试过DanielBrü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
        }));

4 个答案:

答案 0 :(得分:5)

IQueryable<Branch> branches = GetBranches();

var result = braches.
   SelectMany(b => b.Addresses.
      DefaultIfEmpty().
      Select(a => new { Branch = b, Address = a }));

答案 1 :(得分:2)

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

答案 2 :(得分:2)

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

不是Linq最可发现的功能,我害怕。

答案 3 :(得分:0)

对不起,也许我已经很晚了 - 但是我遇到了类似的问题,甚至在这篇帖子发布后的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)));
    }
}