I am trying to find a more efficient way to add additional information to a list from a query. For example, if I have a list of objects with the ObjectID and StringA set, I would like to query the database based on ObjectID to retrieve StringB and StringC:
public class SomeObject {
public int ObjectID { get; set; }
public string StringA { get; set; }
public string StringB { get; set; }
public string StringC { get; set; }
}
public void AddInformationToSomeObjects(List<SomeObject> someObjects)
{
var listOfIDs = someObjects.Select(so => so.ObjectID).ToList();
var informationToAdd = db.Table.Where(t => listOfIDs.Contains(t.ObjectID)).Select(t => new { ObjectID = t.ObjectID, StringB = t.StringB, StringC = t.StringC }).ToList();
foreach (var someObject in someObjects)
{
var information = informationToAdd.Where(i => i.ObjectID == someObject.ObjectID).FirstOrDefault();
someObject.StringB = information.StringB;
someObject.StringC = information.StringC;
}
}
Is there any way to combine the query and the assignment into one statement?
Just update the whole
List…