如何从 linqQuery 从 foreach 循环中获取数组中的多个值?



嗨,我想在 foreach 循环中执行的数组中获取查询的结果,我有我的自定义类,但每当我尝试将其保存在变量中时,它都会显示错误:

无法将通用自定义类转换为我的自定义类

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;

}

这是不可能的,因为在 foreach 循环中,每次在查询中创建匿名类型时,您都无法将它们全部聚合到一个数组中。 为此,最好先获取所有保险应用程序ID,然后在一个查询中获取在其应用程序中具有该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()

相关内容

  • 没有找到相关文章

最新更新