用多个查询填充对象列表

发布于 2024-10-30 07:08:37 字数 584 浏览 1 评论 0原文

我有一个使用组合构建的对象模型,如下所示:

MyModel{
  public int ModelID {get;set;}
  public List<OtherModel1> ListOfOtherModel {get;set;}
  public List<OtherModeln> ListOfOtherModels {get;set;}
  ...
}

我有一个返回 ModelID 列表的 linq-to-sql 查询,我将此集合作为其他查询的输入参数传递,以填充 OtherModels 对象的列表;这些对象通过 ModelID 与 MyModel 相关。当这些查询结束时(我有 9 个查询),我最终得到 10 个列表。

我将这 10 个列表(ListOfMyModel 和其他 9 个:ListOfOtherModel1、ListOfOtherModels...)传递给另一个函数,该函数将通过循环 ListOfMyModel 并查询每个其他列表以查看它们是否包含列表来构建 MyModel 的集合具有相同的 ModelID。

当我做这一切时,我只是想知道是否有更简单/更快/更好的方法来做到这一点。感谢您的建议。

I have an object model that I build with composition and that looks like this:

MyModel{
  public int ModelID {get;set;}
  public List<OtherModel1> ListOfOtherModel {get;set;}
  public List<OtherModeln> ListOfOtherModels {get;set;}
  ...
}

I have a linq-to-sql query that return a list of ModelID and I pass this collection as the input paramater for other queries to populate the lists of OtherModels object; these objects are related to MyModel through the ModelID. When these queries end (I have 9 queries), I end up with 10 lists.

I'm passing those 10 lists (ListOfMyModel and the 9 others : ListOfOtherModel1, ListOfOtherModels...) to another function that'll build a collection of MyModel by looping throught the ListOfMyModel and querying each of the other lists to see if they contain lists with the same ModelID.

As I'm doing all that, I'm just wondering if there's an easier/faster/better way to do it. Thanks for your suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

属性 2024-11-06 07:08:37

如果它们具有共同的基类,则您可以对集合进行强制转换,然后在执行查询之前与每个集合执行并集。

例如,假设 Derived1Derived2 具有公共基类 CommonBaseClass

 IEnumerable<CommonBaseClass> Merge(List<Derived1> derived1list, List<Dervived2> derived2list){
      return derived1list.Cast<CommonBaseClass>().Union(derived2list.Cast<CommonBaseClass>());
 }

因此,您可以合并所有列表,然后对大的 CommonBaseClass 集合,而不是单独查询每个集合然后加入它们。

我认为这就是您正在寻找的东西,但您的问题不是很清楚。如果您发布一些代码,会更容易帮助您。

似乎您可能正在寻找 SelectMany 扩展方法,但我不确定,因为语言不清楚。

If they have a common base class, you cast the collection then perform a union with each of them, before performing the query.

For example, assuming Derived1 and Derived2 have the common base class CommonBaseClass

 IEnumerable<CommonBaseClass> Merge(List<Derived1> derived1list, List<Dervived2> derived2list){
      return derived1list.Cast<CommonBaseClass>().Union(derived2list.Cast<CommonBaseClass>());
 }

So, you could just merge all the lists and then perform the query on the big CommonBaseClass collection instead of the querying each individually then joining them.

I think this is the kind of thing you are looking for, but your question wasn't extraordinarily clear. If you post some code, it would be easier to help you.

It also seemed like you may be looking for the SelectMany extension method, but I'm not sure because the language was unclear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文