2015-05-14 24 views
0

我不知道如何解釋清楚。但是我試圖在LINQ語句執行時獲得另一個屬性。 例如:執行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來完成此操作?

+0

如果我理解正確的話,'WebEnabled'和'Active'從'oApplicationSubcode'屬性,則需要檢查它們是否'每個Y''MemberOnlineCommunityPreferences'對象?如果這是真的,我想你可以在where子句中包含方法調用'get_ApplicationSubCode'並在那裏過濾? –

回答

0

像這樣(完全未經測試)?

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(); 
2

您需要使用「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 
     }; 
相關問題