Hi I want to get result of my query in Array which is executed in foreach loop,i have my custom class but whenever I try to save it in variable it shows error :
it is not possible to convert genriccustom class to my custom class
var userName = HttpContext.Current.User.Identity.Name;
var result = entities.USERS.SingleOrDefault(x => x.USER_NAME.Equals(userName, StringComparison.OrdinalIgnoreCase));
var Result = (from p in entities.Apps where p.USERID == result.USERID select p).ToList();
var children = new List<InsuredSummary>();
foreach (var queryresult in Result)
{
var query_result = (from b in entities.USERS
// join c in entities.APP_INFORMATION on Result.INSURED_APPLICATION_ID equals c.INSURED_APPLICATION_ID
// join d in entities.APP_INFO_VEHICLE on Result.INSURED_APPLICATION_ID equals d.INSURED_APPLICATION_ID
// join e in entities.APP_INFO_VEHICLE_OPTIONS_ACCESSORIES on Result.INSURED_APPLICATION_ID equals e.INSURED_APPLICATION_ID
join f in entities.Apps.Where(y => y.INSURED_APPLICATION_ID == queryresult.INSURED_APPLICATION_ID) on b.USERID equals f.USERID
select new
//USER
// InsuredSummary
{
USERID = b.USERID,
//INSURED_APPLICATION_ID = queryresult.INSURED_APPLICATION_ID,
//User
USER_NAME = b.USER_NAME,
INSURED_FIRST_NAME = b.INSURED_FIRST_NAME,
INSURED_LAST_NAME = b.INSURED_LAST_NAME,
INSURED_EMAIL = b.INSURED_EMAIL,
//App_Information
}).ToList();
// Want to save query_result in Array[]
}
return query_result;
}
It's not possible because in your foreach loop, every time you're creating an anonymous type in your query and you can't aggregate all of them in one array. For doing this it's better to first get all insuredApplicationIDs and after that in one query get all users that have that id in their Apps.
var insuredApplicationIDs = Result.Select(r => r.INSURED_APPLICATION_ID);
var queryResaut = (from b in entities.USERS
join f in entities.Apps.Where(y => insuredApplicationIDs.Contains(y.INSURED_APPLICATION_ID)) on b.USERID equals f.USERID
select new
{
USERID = b.USERID,
//INSURED_APPLICATION_ID = queryresult.INSURED_APPLICATION_ID,
//User
USER_NAME = b.USER_NAME,
INSURED_FIRST_NAME = b.INSURED_FIRST_NAME,
INSURED_LAST_NAME = b.INSURED_LAST_NAME,
INSURED_EMAIL = b.INSURED_EMAIL,
//App_Information
}).ToArray()