C# 使用LINQ中另一个子查询的值填充字段

C# 使用LINQ中另一个子查询的值填充字段,c#,asp.net-mvc,linq,linq-to-sql,C#,Asp.net Mvc,Linq,Linq To Sql,我有以下Linq,我可以像这样使用Linq来获取IsPremiumUser和IsLawFirm的值吗?还是有其他好方法可以做到这一点 我需要的是根据供应商id获取IsPremiumUser和IsLawFirm的值。我不想添加任何foreach来完成这项工作,我可以使用LINQ获取这些值吗 var supplierListQuery = (from sup in db.PPSuppliers select new SearchResultSupplierViewModel

我有以下Linq,我可以像这样使用Linq来获取IsPremiumUserIsLawFirm的值吗?还是有其他好方法可以做到这一点

我需要的是根据供应商id获取IsPremiumUserIsLawFirm的值。我不想添加任何foreach来完成这项工作,我可以使用LINQ获取这些值吗

var supplierListQuery = 
    (from sup in db.PPSuppliers
     select new SearchResultSupplierViewModel
     {
         SupplierId = sup.PPSupplierId,
         SupplierLocation = sup.LocationsForDisplay,
         CreatedDate = (DateTime)sup.CreatedDate,
         PPMemberId = sup.PPMemberId,
         IsPremiumUser =
             (from u in db.PPSubscriptions
              join s in db.PPSubscriptionPlans on u.SubscriptionPlanId equals s.SubscriptionPlanId
              where u.PPMemberId == sup.PPMemberId && u.SubscriptionEndDate >= System.DateTime.Now
              orderby u.SubscriptionStartDate descending
              select s).FirstOrDefault().isPremium,
         IsLawFirm = 
             (from l in db.PPSupplierSupplierTypes 
              join f in db.PPSupplierTypes on l.PPSupplierTypeId equals f.PPSupplierTypeId
              where l.PPSupplierId == sup.PPSupplierId && f.PPSupplierTypeId == 1
              select l).Any()
     });

我会放置一个分析器来查看SQL EF生成了什么,然后我会做出一个决定:如果查询看起来非常复杂,可能会产生性能问题,那么我会尝试做其他事情:

  • 使用“let”关键字
  • 尝试在中间数据结构中加载子查询数据,这些中间数据结构与哈希表相似,哈希表的键上有PPSuppliers Id和值,以及您需要分发的任何数据,然后通过对供应商的单个foreach循环,我将在O(1)中从这些中间结构中窥探我需要添加的其他信息

  • 使用存储过程:)