首页 > 解决方案 > 如何在 linq 的连接条件中使用两个 substring() 方法

问题描述

我正在实施 asp.net 核心项目。我有如下查询:

var numerator = from t1 in _context.Apiapplicant
                            join t2 in _context.ApiApplicantHistory on t1.Id equals t2.ApiApplicantId
                            join t3 in _context.EntityType on t2.LastReqStatus equals t3.Id
                            join t4 in mytotal on new { t2.Date.Substring(0,4), t2.Date.Substring(2,5) } equals new { t4.Year, t4.Month }

……

我的问题是,在最后一行我想在给定条件下加入 mytotal,但是表达式 t2.Date.Substring(0,4), t2.Date.Substring(2,5) } 等于 new { t4.Year , t4.Month } ... 出现错误。如果有人告诉我如何写这样的条件,我将不胜感激。

标签: c#linqasp.net-core

解决方案


当你调用一个方法来创建匿名对象的成员时,你需要将它分配给成员名称,否则会报错:

无效的匿名类型成员声明符。匿名类型成员必须使用成员分配、简单名称或成员访问来声明

将最后一行更改为:

join t4 in mytotal on new { Year = t2.Date.Substring(0,4), Month = t2.Date.Substring(2,5) } equals new { t4.Year, t4.Month }

我希望你觉得这有帮助。


推荐阅读