LINQ In Line Property Update During Join

后端 未结 6 1419
难免孤独
难免孤独 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:01

    From the word "tables", it sounds like you are getting this data from a database. In which case; no: you can't do this. The closest you can do would to select the objects and the extra columns, and update the properties afterwards:

    var qry = from objectA in GetObjectAs()
              join objectB in GetObjectBs()
                 on objectA.Id equals objectB.AId
              select new { A = objectA,
                  objectB.SomeProp, objectB.SomeOtherProp };
    
    foreach(var item in qry) {
        item.A.SomeProp = item.SomeProp;
        item.A.SomeOtherProp = item.SomeOtherProp;
        // perhaps "yield return item.A;" here
    }
    

    If you were doing LINQ-to-Objects, there are perhaps some hacky ways you could do it with fluent APIs - not pretty, though. (edit - like this other reply)

提交回复
热议问题