【问题标题】:How to query all child objects of a list for matching entities?如何查询列表的所有子对象以查找匹配的实体?
【发布时间】:2013-08-24 09:32:10
【问题描述】:

在 LINQ 和 lambda 表达式方面我有点新手,我希望有人可以帮助我。

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

记录集如下所示:

美食
FooId
...其他描述性字段

酒吧
酒吧标识
FooId
...其他描述性字段

巴兹
BazId
酒吧标识
...其他描述性字段

我正在遍历每个数据表,一次一个。处理完第一个表后,我有了所有的“Foos”,然后第二个是“Bars”,我可以毫无困难地将其关联到“Foo”。 (一个“Bar”可以分配给多个 Foo)。

我正在补水的对象是一个列表(为了通过网络服务返回它),对象看起来像这样(伪代码):

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”(反过来可以附加到多个“Foo”)

到目前为止,我的代码看起来像这样。如果有更好的方法来解决这个问题,请告诉我。 我的麻烦是,当我进入处理“Baz”的部分时,我不确定如何选择所有“Bar”对象(在任何具有该 ID 的 Bar 的 Foo 下),因此我可以添加当前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);
            }
        }
    }
}

非常感谢任何帮助。

【问题讨论】:

  • 可以附加到多个“栏” - 不使用给定的表格定义。确保您清楚 1-to-N 和 N-to-M 关系,然后放弃并使用 Entity Framework。
  • @HenkHolterman 为什么它不能连接到多个酒吧?
  • @HenkHolterman -- 这些不是表格,它们是查询输出。如果它是表定义,那将是 1-1 关系,您是正确的,但给定的 BazId 有多行。 :)
  • 不是 1-1 而是 1-N。注意这些基数,它们很重要。

标签: c# linq lambda


【解决方案1】:

使用SelectMany (MSDN)

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);
            }

更新为 a.Bars 可能为空;

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);
            }

【讨论】:

  • Bob -- 这是完美的,似乎完全符合我的要求。不敢相信我离得这么近!也感谢有关空检查的更新。我也把它放进去。
猜你喜欢
  • 1970-01-01
  • 2017-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-07
  • 1970-01-01
相关资源
最近更新 更多