Skip to main content
 首页 » 编程设计

c#之LINQ 使用扩展方法语法连接具有不同记录的多列

2024年02月05日19xiaohuochai

<分区>

抱歉,如果我的问题没有多大意义。让我解释一下。

我有一个现有的 SQL 查询如下:

 SELECT  
     T1.Portfolio, T1.ValueDate as CurrentValueDate,  
     T1.DifferencePercent as CurrentDP,  
     T2.ValueDate as PreviousValueDate,  
     T2.DifferencePercent as PreviousDP, 
     CASE  
        WHEN T1.DifferencePercent = T2.DifferencePercent  
          THEN 1  
          ELSE 0  
     END as IsChange 
 FROM  
     [AccountingData].[dbo].[NAVRec_NAVSummary] T1 
 JOIN  
     [AccountingData].[dbo].[NAVRec_NAVSummary] T2 ON T1.Portfolio = T2.Portfolio AND T2.ValueDate = DATEADD(DAY, CASE DATENAME(WEEKDAY, T1.ValueDate) WHEN 'Sunday' THEN -2 WHEN 'Monday' THEN -3 ELSE -1 END, DATEDIFF(DAY, 0, T1.ValueDate)) 

基本上,我感兴趣的是显示投资组合以及一列,该列说明投资组合的 DifferencePercent 值是否与前一个工作日相同投资组合的值发生变化。

我正在尝试使用扩展方法语法将相同的 sql 查询转换为 LINQ。但是,现在我只是尝试使用 Portfolio 属性和今天投资组合的 ValueDate 以及同一投资组合在前一个工作日的 ValueDate 进行 self 连接。

var result = NavList.Join(NavList,  
                            outer => new { outer.Portfolio, outer.ValueDate },  
                            inner => new { inner.Portfolio, PD = inner.ValueDate.PreviousBusinessDate() },  
                            (outer, inner) => new { outer, inner }); 

[PreviousBusinessDate() 是一个返回 DateTime 的扩展方法]

在这里,我得到一个错误:

The type arguments for method 'System.Linq.Enumerable.Join(System.Collections.Generic.IEnumerable, System.Collections.Generic.IEnumerable, System.Func, System.Func, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

谁能帮我理解问题和解决方案。