James123
James123

Reputation: 11662

Get another object while LINQ executing

I don't know how to explain this clearly. But I am trying to get another properties while LINQ statement is executing. example:

This is a working LINQ Statement:

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

Now, I need to pass DemographicCodeString and DemographicSubcodeString values to

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

And, check properties WebEnabled =="Y" and Active=="Y", then only read customerData and assign it to results. Is it possible to do this with LINQ in a single statement?

Upvotes: 0

Views: 100

Answers (2)

Steve Czetty
Steve Czetty

Reputation: 6238

Something like this (completely untested)?

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

Upvotes: 0

Jeno Laszlo
Jeno Laszlo

Reputation: 2301

You need to use the "let" keyword.

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
       };

Upvotes: 2

Related Questions