tal tal - 3 months ago 34
C# Question

Get a list of entities by list of guids

I am trying to get a partial list of entities for a list of entities by their guids.

could be accomplished by:

List<Entity> GetEntitiesByGuids (List<Entity> entities, List<Guids> guids)
{
List<Entities> retVal = new List<Entities>();
foreach(var guid in guids)
{
var Entity = Entities.Find(e=>e.Guid ==guid)
retVal.Add(Entity);
}
return retVal;
}


Is there any nice way to do it without foreach?

TX

Answer
List<Entity> GetEntitiesByGuids (EntityList entities, List<Guids> guids)
{
    List<Entities> retVal = Entities.Where(e => guids.Contains(e.Guid)).ToList();
    return retVal;
}