LINQ 使用扩展方法语法连接具有不同记录的多列
LINQ Join on multiple columns with different record using extension method syntax
抱歉,如果我的问题没有多大意义。让我解释一下。
我有一个现有的 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。但是,现在我只是尝试使用投资组合 属性 和今天投资组合的 ValueDate 以及同一投资组合在上一个营业日的 ValueDate 进行自我连接。
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() 是 returns 日期时间的扩展方法]
在这里,我得到一个错误:
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.
谁能帮我理解问题和解决方案。
问题就在这里
outer => new { outer.Portfolio, outer.ValueDate },
inner => new { inner.Portfolio, PD = inner.ValueDate.PreviousBusinessDate() },
您正在投影匿名类型,它们需要完全匹配。为了匹配,匿名类型必须具有完全相同的签名,其中包括 属性 name 及其 type。
在您的情况下,您没有明确指定大多数 属性 名称,因此以上等同于
outer => new { Portfolio = outer.Portfolio, ValueDate = outer.ValueDate },
inner => new { Portfolio = inner.Portfolio, PD = inner.ValueDate.PreviousBusinessDate() },
看出区别了吗?第一行中的第二个成员称为 ValueDate
而在第二行中 - PD
.
只需将 PD
替换为 ValueDate
即可解决问题(暂时)。
抱歉,如果我的问题没有多大意义。让我解释一下。
我有一个现有的 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。但是,现在我只是尝试使用投资组合 属性 和今天投资组合的 ValueDate 以及同一投资组合在上一个营业日的 ValueDate 进行自我连接。
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() 是 returns 日期时间的扩展方法]
在这里,我得到一个错误:
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.
谁能帮我理解问题和解决方案。
问题就在这里
outer => new { outer.Portfolio, outer.ValueDate },
inner => new { inner.Portfolio, PD = inner.ValueDate.PreviousBusinessDate() },
您正在投影匿名类型,它们需要完全匹配。为了匹配,匿名类型必须具有完全相同的签名,其中包括 属性 name 及其 type。
在您的情况下,您没有明确指定大多数 属性 名称,因此以上等同于
outer => new { Portfolio = outer.Portfolio, ValueDate = outer.ValueDate },
inner => new { Portfolio = inner.Portfolio, PD = inner.ValueDate.PreviousBusinessDate() },
看出区别了吗?第一行中的第二个成员称为 ValueDate
而在第二行中 - PD
.
只需将 PD
替换为 ValueDate
即可解决问题(暂时)。