开发者

Filling an object list with multiple queries

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜