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

我这样输入矩阵数据:

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


因此记录将类似于:

客户A:3客户B:1001

客户A:4个客户B:1001

客户A: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_customer_no:227,tophat_customer_detail_Id 1

记录2:datatrac_customer_no:228,tophat_customer_detail_Id:1

记录3:datatrac_customer_no:910,tophat_customer_detail_Id:5

然后对于拉入mainPull的第一条记录,字段c.customer_no == 228,因此我需要在select new语句中查询以将th_customerId替换为1(来自Matrix的记录2)。

然后说下一条记录拉入mainPull字段c.customer_no = 910,th_customerId为5。

这就是我的foreach语句的第一行当前正在执行的操作。我想将该逻辑移到我的LINQ查询中。

最佳答案

如果我对您的理解正确,那么在此处使用带有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


如果找不到或存在多次键,则两个表达式都将引发异常-但是,如果我正确理解您的结构,则应该无法实现。否则,您需要检查一下。

10-04 12:35
查看更多