C# 在LINQ查询中添加Lambda以替换foreach中的行

C# 在LINQ查询中添加Lambda以替换foreach中的行,c#,linq,lambda,C#,Linq,Lambda,我的情况是,我必须将一个系统中的多个客户编号与另一个系统中的单个客户编号进行匹配。 例如,系统A中的客户号225、228和223将全部映射到系统B中的客户号110022。 很简单,我有一个矩阵设置来实现这一点 我将矩阵数据按如下方式拉入:  var dt_th_matrix = (from m in aDb.Matrix_Datatrac_TopHat select m).ToArray(); foreach (var dt_stop in mainPull) {

我的情况是,我必须将一个系统中的多个客户编号与另一个系统中的单个客户编号进行匹配。 例如,系统A中的客户号225、228和223将全部映射到系统B中的客户号110022。 很简单,我有一个矩阵设置来实现这一点

我将矩阵数据按如下方式拉入:

 var dt_th_matrix = (from m in aDb.Matrix_Datatrac_TopHat select m).ToArray();
foreach (var dt_stop in mainPull)
        {
            int? th_customerId = (from d in dt_th_matrix 
                                  where d.datatrac_customer_no == dt_stop.Customer_No.ToString() 
                                  select d.tophat_customer_detail_Id).First();
  th_customerId = (dt_th_matrix.First().tophat_customer_detail_Id.Equals c.Customer_No)
所以这些记录应该是这样的:

客户编号:3客户编号:1001

客户编号:4客户编号:1001

客户编号:5客户编号:1002

然后,我做了一个大数据拉和通过所有项目的步骤。对于每个项目,我都会从矩阵中获取匹配的客户编号,如下所示:

 var dt_th_matrix = (from m in aDb.Matrix_Datatrac_TopHat select m).ToArray();
foreach (var dt_stop in mainPull)
        {
            int? th_customerId = (from d in dt_th_matrix 
                                  where d.datatrac_customer_no == dt_stop.Customer_No.ToString() 
                                  select d.tophat_customer_detail_Id).First();
  th_customerId = (dt_th_matrix.First().tophat_customer_detail_Id.Equals c.Customer_No)
我更愿意做的是将代码嵌入到我的datapull中,直接从矩阵中获取客户编号——这里的部分查询可能是某种类型的Lambda。有什么帮助吗

我试过这样的方法:

 var dt_th_matrix = (from m in aDb.Matrix_Datatrac_TopHat select m).ToArray();
foreach (var dt_stop in mainPull)
        {
            int? th_customerId = (from d in dt_th_matrix 
                                  where d.datatrac_customer_no == dt_stop.Customer_No.ToString() 
                                  select d.tophat_customer_detail_Id).First();
  th_customerId = (dt_th_matrix.First().tophat_customer_detail_Id.Equals c.Customer_No)
但显然不是这样

var mainPull = (from c in cDb.DistributionStopInformations
                        join rh in cDb.DistributionRouteHeaders on c.Route_Code equals rh.Route_Code
                        where c.Company_No == 1 &&
                       (accountNumbers.Contains(c.Customer_No)) &&
                         (brancheSearchList.Contains(c.Branch_Id) && brancheSearchList.Contains(rh.Branch_Id)) &&
                        c.Shipment_Type == "D" &&
                       (c.Datetime_Created > dateToSearch || c.Datetime_Updated > dateToSearch) &&
                       rh.Company_No == 1 &&
                       ((rh.Route_Date == routeDateToSearch && c.Route_Date == routeDateToSearch) ||
                       (rh.Route_Date == routeDateToSearch.AddDays(1) && c.Route_Date == routeDateToSearch.AddDays(1)))
                        orderby c.Unique_Id_No
                        select new
                        {
                            c.Datetime_Updated,
                            th_customerId = ("Query goes here somehow")
                            c.Datetime_Created,
                            c.Unique_Id_No,
                            c.Original_Unique_Id_No,
                            c.Unique_Id_Of_New_Stop,
                            c.Branch_Id,
                            c.Route_Date,
                            c.Route_Code,
                            c.Sequence_Code,
                            c.Customer_No,
                            c.Customer_Reference,
                            c.Shipment_Type,
                            c.Stop_Name,
                            c.Stop_Address,
                            c.Stop_City,
                            c.Stop_State,
                            c.Stop_Zip_Postal_Code,
                            c.Stop_Phone_No,
                            c.Stop_Arrival_Time,
                            c.Stop_Departure_Time,
                            c.Address_Point,
                            c.Stop_Special_Instruction1,
                            c.Stop_Special_Instruction2,
                            c.Stop_Expected_Pieces,
                            c.Stop_Expected_Weight,
                            c.Stop_Signature,
                            c.Actual_Arrival_Time,
                            c.Actual_Depart_Time,
                            c.Actual_Service_Date,
                            c.Stop_Actual_Pieces,
                            c.Stop_Exception_Code,
                            c.Created_By,
                            rh_Route_Date = rh.Route_Date,
                            routeHeaderRouteCode = rh.Route_Code,
                            rh.Actual_Driver,
                            rh.Assigned_Driver,
                            rh_routeDate = rh.Route_Date

                        }).ToArray();
我将努力澄清上述问题

我需要的是Linq查询说: 对于我提取的每个记录,我将转到名为dt_th_matrix的数组,获取与此行匹配的记录并使用它

矩阵中的数据如下所示:

 var dt_th_matrix = (from m in aDb.Matrix_Datatrac_TopHat select m).ToArray();
foreach (var dt_stop in mainPull)
        {
            int? th_customerId = (from d in dt_th_matrix 
                                  where d.datatrac_customer_no == dt_stop.Customer_No.ToString() 
                                  select d.tophat_customer_detail_Id).First();
  th_customerId = (dt_th_matrix.First().tophat_customer_detail_Id.Equals c.Customer_No)
记录1:datatrac\u客户编号:227,tophat\u客户详细信息\u Id 1

记录2:datatrac\u客户编号:228,tophat\u客户详细信息\u Id:1

记录3:datatrac\u客户编号:910,tophat\u客户详细信息\u Id:5

然后,对于在mainPull中提取的第一条记录,字段c.customer\u no==228,因此我需要select new语句中的查询将th\u customerId替换为矩阵中记录2中的1

然后,假设在主拉字段中拉取的下一个记录c.customer_no=910,则th_customerId将为5


这就是我的foreach语句的第一行目前正在做的事情。我想将该逻辑移到我的LINQ查询中。

如果我理解正确,在这里使用具有datatrac\u customer\u no键和tophat\u customer\u detail\u Id值的字典将是一个好主意:

var dt_th_matrix = (from m in aDb.Matrix_Datatrac_TopHat select m).ToDictionary(m=>m.datatrac_customer_no,m=>m.tophat_customer_detail_Id);
有了它,您应该能够用

dt_th_matrix[c.Customer_No]
使用LINQ也是可能的,但我认为它不值得性能开销和可读性的降低

如果您仍然希望将LINQ用于原始矩阵,则应将其用作查询:

dt_th_matrix.Single(m => m.datatrac_customer_no == c.Customer_No).tophat_customer_detail_Id

如果找不到键或键多次存在,这两个表达式都会引发异常-但如果我正确理解了您的结构,则不可能发生这种情况。否则,您需要对此进行检查。

我更容易理解一些示例数据,因此我不太确定您的语法,但我认为您首先需要在id上使用group by子句,然后再加入其中:我添加了澄清和更多数据。我通过你提供的链接浏览了整个小组,但我觉得不是这样。谢谢你-我从来没有想过使用字典,好主意。