LINQ In Line Property Update During Join

后端 未结 6 1433
难免孤独
难免孤独 2021-02-05 11:55

I have two obects, A & B for this discussion. I can join these objects (tables) via a common relationship or foreign key. I am using linq to do this join and I only want t

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-05 12:06

    Add an update method to your ClassA

    class ClassA {
      public ClassA UpdateWithB(ClassB objectB) {
        // Do the update
        return this;
      }
    }
    

    then use

    return from objectA in GetObjectAs()
       join objectB in GetObjectBs()
               on objectA.Id equals objectB.AId
               // update object A with object B data before selecting it
       select objectA.UpdateWithB(objectB);
    

    EDIT:

    Or use a local lambda function like:

    Func f = ((a,b)=> { a.DoSomethingWithB(b); return a;});
    return from objectA in GetObjectAs()
           join objectB in GetObjectBs()
           on objectA.Id equals objectB.AId
           select f(objectA , objectA );
    

提交回复
热议问题