MrBliz
MrBliz

Reputation: 5898

Linq Where list Contains item in another list

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>()?

Upvotes: 5

Views: 19062

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460028

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 )

Upvotes: 11

Related Questions