代码之家  ›  专栏  ›  技术社区  ›  Joe Ruder

在Linq查询中添加lambda以替换foreach中的行

  •  3
  • Joe Ruder  · 技术社区  · 6 年前

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

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

     var dt_th_matrix = (from m in aDb.Matrix_Datatrac_TopHat select m).ToArray();
    

    所以记录应该是这样的:

    客户:3客户:1001

    客户:4客户:1001

    客户:5客户:1002

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

    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();
    

    我更愿意做的是将代码直接嵌入到我的数据拉取中,从矩阵中获取客户编号——“查询不知何故地出现在这里”这一部分将是我假定的某种类型的lambda。有什么帮助吗?

    我试过这样的方法:

      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的数组,并获取与此行匹配的记录并使用它。

    矩阵中的数据如下所示:

    记录1:datatrac_客户编号:227,Tophat_客户详细信息_ID 1

    记录2:datatrac_客户编号:228,Tophat_客户详细信息:1

    记录3:datatrac_客户编号910,Tophat_客户详细信息ID:5

    然后,对于在mainpull中提取的第一条记录,字段c.customer_no==228,因此我需要在select new语句中进行查询,以将该customerID替换为1(来自矩阵中的记录2)。

    然后说在mainpull中拉入的下一个记录在c.customer_no=910字段中,则th_customerID将为5。

    这就是我的foreach语句的第一行当前正在做的。我想把这个逻辑转移到我的LINQ查询中。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Christoph Sonntag    6 年前

    如果我正确理解您的意思,那么在这里使用一个带有datatrac_customer_no和tophat_customer_detail_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
    

    如果键未找到或多次存在,这两个表达式都将抛出异常-但是如果我正确理解了您的结构,这就不可能实现。否则你需要检查一下。