您好我希望在foreach循环中执行我的查询结果,我有自定义类但是每当我尝试将其保存在变量中时它显示错误:
无法将genriccustom类转换为我的自定义类
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;
}
答案 0 :(得分:1)
这是不可能的,因为在您的foreach循环中,每次您在查询中创建匿名类型时,您都无法在一个数组中聚合所有这些类型。 为此,最好首先获取所有insuredApplicationID,然后在一次查询中获取在其应用中具有该ID的所有用户。
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()