简体   繁体   中英

Adding additional information to a list from a linq-to-entities query

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 ...

public void AddInformationToSomeObjects(IEnumerable<SomeObject> someObjects)
{
    someObjects =
        from obj in someObjects
        join dbObj in db.Table
        on obj.ObjectID equals dbObj.ObjectID
        select new SomeObject
        {
            ObjectID = obj.ObjectID,
            StringA = obj.StringA,
            StringB = dbObj.StringB,
            StringC = dbObj.StringC
        }
}

As Jan P. says it may not be a big deal to just re-fetch a new list. But it getting the first list involved some expensive operation (like fetching blobs) your idea is not bad at all. As simple join would do:

public List<SomeObject> 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();

    return(
        from org in someObjects
        join a in informationToAdd on org.ObjectID equals a.ObjectID
        select new SomeObject {
                                ObjectID = org.ObjectID,
                                StringA = org.StringA,
                                StringB = a.StringB,
                                StringC = a.StringC
                            }).ToList();
}

Returning a new list and not changing the original one is just a personal preference (minimize side-effects of methods/functions).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM