C# linq仅查询包含详细信息的标题

C# linq仅查询包含详细信息的标题,c#,linq,lambda,C#,Linq,Lambda,我试图使用这个Lambda语句仅获取包含详细信息的标题 list = list.Where(c=> c.CustomerSalesPeople.Count>0); return list != null ? list.Where(c=> c!=null && c.CustomerSalesPeople.Count>0). OrderBy(c => c.CustomerName).ToList() :null; 但是当我试图返回

我试图使用这个Lambda语句仅获取包含详细信息的标题

list = list.Where(c=> c.CustomerSalesPeople.Count>0);
return list != null ? 
   list.Where(c=> c!=null && c.CustomerSalesPeople.Count>0).
   OrderBy(c => c.CustomerName).ToList()
   :null;
但是当我试图返回结果时,我得到了一个空异常

return list.OrderBy(c => c.CustomerName).ToList();
我已经仔细阅读了代码,看到在我执行第一条语句之后立即生成了null异常。有没有更好的方法来实现这一点

编辑

我尝试了这些建议,但仍然得到一个空值。我想我应该试着更好地解释。我需要一个匹配此查询的Lambda表达式

    SELECT *
  FROM [customer]
  where customer_record_id in (select distinct(customer_id)from customer_sales_person)
或者,如果您认为,
CustomerSalesPeople
可以为空,则可以执行以下操作:

list = list.Where(c=>(c.CustomerSalesPeople==null) ||(c.CustomerSalesPeople.Count>0));

if(list!=null)
  return list.OrderBy(c=>c.CustomerName).ToList();

return list;
您还可以查看
.DefaultIfEmpty()
扩展名

Linq提供了优秀的扩展方法,可以在发现空结果集时进行计数。 这是:

  • 更新: 这样做:

       List<int> uniqueIds= listOfCustomerSalesPerson.Select(s=>s.Id).ToList().Distinct();
       var requireListOfCustomers = GetAllCustomers().Where(s=>uniqueIds.Contains(s.id);
    

    我假设,Customer包含
    列表
    ,它可以是
    db。CustomerSalesPeople
    否则

    您的集合中可以有空元素。尝试检查where语句中的空值

    list = list.Where(c=> c.CustomerSalesPeople.Count>0);
    
    return list != null ? 
       list.Where(c=> c!=null && c.CustomerSalesPeople.Count>0).
       OrderBy(c => c.CustomerName).ToList()
       :null;
    

    它是对象/sql/实体的linq吗?它是对象的linq。它们是llblgen实体。LLBLGen是ORM,它将Select更改为Any,工作非常完美。我不认为我知道将父变量s作为参数传递给子查询。谢谢