Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq Where list Contains item in another list

Tags:

c#

.net

linq

Given the following Structure

public class WorkOrderItem 
{
    [Key] 
    public long WorkOrderItemId { get; set; }
    public virtual ICollection<Job> Jobs { get; set; }      
}

public class Job 
{
    [Key]
    public long JobId { get; set; }
    public long? WorkOrderItemId { get; set; }
    public virtual Item Item { get; set; }
    public virtual Element ResultElement { get; set; }
}

How would i get a list of Items where the item had a job that the ResultElementid was in a List<long>()?

like image 523
MrBliz Avatar asked Dec 25 '22 03:12

MrBliz


1 Answers

You can use Any + Contains:

var query = workOrderItems
    .Where(item => item.Jobs.Any(j => longList.Contains(j.ResultElement.Id)));

( presuming that the class Element has an Id property since you've stated ResultElementid )

like image 76
Tim Schmelter Avatar answered Dec 27 '22 18:12

Tim Schmelter