Sveta23
Sveta23

Reputation: 13

Setting values in LINQ query

I have a project where i need to load results from one entity select results and than with those results to load another 2 entities and select from them data wich will be return later.

is it possiable to build LINQ Query from 3 repositories ? please give me some code examples or point me to links...i cannot find how

this is just a doodle of something that i ask if it possiable:

  ARepository rep = new AReository();
    BRepository rep = new BReository();
      var Q1= rep.GetAllView1()
                      .Select(o=>new {
                        X=o.data.aaa,
                        Y=o.data.bbb,
    ....
      var Q2= rep.GetAllView2()
                        .Where(x => x.bata.ss == X

      var Q3= rep.GetAllView3()
                        .Where(x => x.cata.ff== Y

Upvotes: 1

Views: 119

Answers (1)

kmp
kmp

Reputation: 10865

I think you would need to use navigation properties for this to be in any way efficient. What you show is possible, but you are very likely to end up getting all the entities back when you do not need them. It looks from your example that you have three entities:

View1

  • aaa
  • bbb

View2

  • ss -> foreign key relationship to View1.aaa

View3

  • ff -> foreign key relationship to View1.bbb

The easiest is to have these keys defined in the database but you can define them in the entity framework designer. Once you have the relationships modelled, it makes things really easy...

So the View1 entitiy could look like this:

public class View1
{
    public ICollection<View2> View2s { get; set; }

    public ICollection<View3> View3s { get; set; }
}

Once you have such an entity the following code would work:

rep
    .GetAllView1()
    .Where(o => o.View2s != null && 
           o.View2s.Count() > 0 && 
           o.View3s != null && 
           o.View3s.Count() > 0)

By this point you have a collection of View1 objects, all with 2 properties with their associated View2 and View3 entities.

Upvotes: 1

Related Questions