如何过滤父列表<>在小孩名单上申请条件的时候?

时间:2014-11-12 08:16:55

标签: .net linq list

如何过滤父列表<>在小孩名单上申请条件的时候?

public class VendorEntities
{
    public int VendorId { get; set; }     
    public string VendorName { get; set; }
    public List<ItemEntities> ItemDealdetails { get; set; }    
}

public class ItemEntities
{
    public int ItemId { get; set; }
    public string ItemName { get; set; }
    public int TenantId { get; set; }
}

List<VendorEntities> objVenderList = new List<VendorEntities>();
objVenderList = objVendorBL.GetVendorName();

var rList = (from a in objVenderList
             from b in a.ItemDealdetails
             where b.ItemName.Contains("Computer")
             orderby a.VendorName descending
             select a).ToList().Distinct();

现在,我想搜索那些ItemName包含Computer的供应商,该供应商存储在子列表ItemEntities中。我怎么能这样做?请给我一些建议。

1 个答案:

答案 0 :(得分:2)

您可以使用Enumerable.Any

var rList = objVenderList
        .Where(v => v.ItemDealdetails.Any(d => d.ItemName.Contains("Computer")))
        .OrderByDescending(v => v.VendorName)
        .ToList();

请注意,我已经省略了您的Distinct,因为它只会删除重复的引用,直到您没有实施IEquatable<T>或覆盖Equals + GetHashCode

这里的查询语法相同:

var filteredByItemNameComputer = 
    from vendor in objVenderList
    where vendor.ItemDealdetails.Any(d => d.ItemName.Contains("Computer"))
    orderby  vendor.VendorName descending
    select vendor;
var rList=filteredByItemNameComputer.ToList();