C# 如何查询列表的所有子对象以查找匹配的实体?

C# 如何查询列表的所有子对象以查找匹配的实体?,c#,linq,lambda,C#,Linq,Lambda,对于LINQ和lambda表达式,我是个新手,我希望有人能帮我 我所做的是从ADO.NET SqlDataReader创建一个聚合对象,响应中有多个数据“表” 记录集如下所示: 福 食物 …其他描述性字段 酒吧 巴里德 食物 …其他描述性字段 巴兹 巴齐德 巴里德 …其他描述性字段 我一次遍历一个数据表。在我处理完第一个表之后,我有了所有的Foo,然后第二个是条,我可以毫无困难地将其关联到Foo。一个条形图可以指定给多个Foo 我正在水合的对象是一个列表,用于通过web服务返回它,对象看起来像以

对于LINQ和lambda表达式,我是个新手,我希望有人能帮我

我所做的是从ADO.NET SqlDataReader创建一个聚合对象,响应中有多个数据“表”

记录集如下所示:

福 食物 …其他描述性字段

酒吧 巴里德 食物 …其他描述性字段

巴兹 巴齐德 巴里德 …其他描述性字段

我一次遍历一个数据表。在我处理完第一个表之后,我有了所有的Foo,然后第二个是条,我可以毫无困难地将其关联到Foo。一个条形图可以指定给多个Foo

我正在水合的对象是一个列表,用于通过web服务返回它,对象看起来像以下伪代码:

class Foo
{
  int FooId
  string Name
  string etc
  string YouGetTheIdea
  List<Bar> Bars
}

class Bar
{
  int BarId
  string etc
  List<Baz> Bazes
}

class Baz
{
  int BazId
  string etc
}
到目前为止,我很好。我遇到的麻烦是,Bar对象有一个Baz对象列表,这些对象可以附加到多个Bar,而Bar对象又可以附加到多个Foo

到目前为止,我的代码是这样的。如果有更好的方法,请告诉我。我的问题是,当我进入处理Baz的部分时,我不知道如何选择任何具有该Id的Bar的Foo下的所有Bar对象,以便将当前BazId添加到其Baz对象列表中。我现在在这里看到的是在运行时爆炸,所以这显然是不对的

using (SafeDataReader reader = this.ExecSPReader(SP_NAME, parms.ToArray()))
{
    if (reader != null)
    {
        // deal with Foos
        while (reader.Read())
        {
            Foo o = new Foo();
            o.FooID = reader.GetInt64("FooID");
            o.Etc = reader.GetString("etc");
            //...more properties
            fooList.Add(o);
        }

        // deal with Bars
        reader.NextResult();
        while (reader.Read())
        {
            Bar p = new Bar();
            long BarFooID = reader.GetInt64("FooID");

            p.BarID = reader.GetInt64("BarID");
            //...more properties

            // find Foo that has this Bar and add to it
            Foo o = fooList.Find(x => x.FooID == barFooID);
            if (o != null)
            {
                if (o.Bars == null)
                {
                    o.Bars = new List<Bar>();
                }
                o.Bars.Add(p);
            }
        }
/*
***
***  Up to here, everything is fine
***  ...but now we need to assign the baz elements to bars, which can belong 
***     to any/all Foos
***
*/
        // deal with Bazs
        reader.NextResult();
        while (reader.Read())
        {
            long bazID = reader.GetInt64("BazID");
            long barID = reader.GetInt64("BarID");

            // here is the problem, ideally I'd like to get a list of all Bar elements with
            // the BarID from the datarow, but not sure how to do that -- the below foreach
            // line errors at runtime
            foreach(Bar p in fooList.Select(a => a.Bars.Where(b => b.BarID == barID)))
            {
                if (p.Bazes == null)
                {
                    p.Bazes = new List<Baz>();
                }
                p.Bazes.Add(bazID);
            }
        }
    }
}
非常感谢您的帮助。

使用

更新为a。条形图可能为空


它可以附加到多个条上,而不是给定的表格定义。确保清楚1对N和N对M关系,然后放弃并使用实体框架。@HenkHolterman为什么不能将其附加到多个条?@HenkHolterman-这些不是表,它们是查询输出。如果是表def,则是1-1关系,但给定的BazId有多行:不是1-1而是1-N。注意这些基数,它们很重要。鲍勃-这很完美,似乎正是我想要的。真不敢相信我离得这么近!还感谢您对空检查的更新。我也把它放进去了。
foreach(Bar p in fooList.SelectMany(a => a.Bars.Where(b => b.BarID == barID)))
            {
                if (p.Bazes == null)
                {
                    p.Bazes = new List<Baz>();
                }
                p.Bazes.Add(bazID);
            }
foreach(Bar p in fooList.Where(a => a.Bars !=null).SelectMany(a => a.Bars.Where(b => b.BarID == barID)))
            {
                if (p.Bazes == null)
                {
                    p.Bazes = new List<Baz>();
                }
                p.Bazes.Add(bazID);
            }