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

How to get multiple value in Array from linqQuery from foreach loop?

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

无法将 genriccustom class 转换为我的自定义 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;


                }

这是不可能的,因为在您的 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()