在LINQ执行时获取另一个对象

本文关键字:一个对象 获取 LINQ 执行 | 更新日期: 2023-09-27 17:49:19

我不知道如何解释清楚。但我试图得到另一个属性,而LINQ语句正在执行。例子:

这是一个工作的LINQ语句:

if (customerData.Demographics.Count > 0)
{
    string[] communities = {"ONLINE_TECH_COMM", "ONLINE_PROF_NET", "ONLINE_TECH_SECTIONS"};
    var results = (from CustomerDemographic customerDemographic in customerData.Demographics where (customerDemographic.UserD2==DateTime.MinValue)
           select new Models.MemberOnlineCommunityPreferences()
           {
               DemographicCode = customerDemographic.DemographicCodeString,
               DemographicSubCode = customerDemographic.DemographicSubcodeString,
     }).Where(x=>communities.Contains(x.DemographicCode)).OrderBy(x=>x.DemographicCode).ToList();

现在,我需要将DemographicCodeStringDemographicSubcodeString的值传递给

oApplicationSubcode = TIMSS.API.CachedApplicationData.ApplicationDataCache.get_ApplicationSubCode("CUS", "DEMOGRAPHIC", DemographicCodeString, DemographicSubcodeString); 

并且,检查属性WebEnabled =="Y"Active=="Y",然后只读取customerData并将其分配给结果。是否有可能在单个语句中使用LINQ来做到这一点?

在LINQ执行时获取另一个对象

你需要使用"let"关键字。

https://msdn.microsoft.com/en-us/library/bb383976.aspx

var results = from CustomerDemographic customerDemographic in customerData.Demographics
let code = customerDemographic.DemographicCodeString
let subcode = customerDemographic.DemographicSubcodeString
where communities.Contains(code) && customerDemographic.UserD2==DateTime.MinValue
let appSubCode = TIMSS.API.CachedApplicationData.ApplicationDataCache.get_ApplicationSubCode("CUS", "DEMOGRAPHIC", code, subcode)
where appSubCode.WebEnabled =="Y" && appSubCode.Active=="Y"
select new Models.MemberOnlineCommunityPreferences()
       {
           DemographicCode = code,
           DemographicSubCode = subcode
       };

像这样的东西(完全未经测试)?

var results = (from CustomerDemographic customerDemographic in customerData.Demographics
               where (customerDemographic.UserD2==DateTime.MinValue)
                  && communities.Contains(x.DemographicCodeString)
               let applicationSubcode = TIMSS.API.CachedApplicationData.ApplicationDataCache.get_ApplicationSubCode("CUS", "DEMOGRAPHIC", customerDemographic.DemographicCodeString, customerDemographic.DemographicSubcodeString)
               where applicationSubcode.WebEnabled == "Y"
                  && applicationSubcode.Active == "Y"
               orderby customerDemographic.DemographicCodeString
               select customerDemographic).ToList();