c# - 无法在 List<anonymous> LINQ 中应用 Union

标签 c# .net linq

我正在尝试将 LINQ 查询的两个结果与我的 DbContext 合并:

var innerJoin = (from pg in context.TbPlageAccueil
                             join pf in context.TbPermanence on pg.IdPlageAccueil equals pf.IdPlage
                             where pf.HeureDebPerm > new DateTime(2016, 10, 1) && pf.HeureFinPerm < new DateTime(2016, 12, 31)
                             select new{ Libelle_Activite = pg.LibellePlageAccueil, Heure_Debut_Activite = pf.HeureDebPerm, Heure_Fin_Activit = pf.HeureFinPerm }).ToList();

var reunion= (from ru in context.TbReunion select new { Libelle_Activite = ru.IntituleReunion, Heure_Debut_Activite = ru.HeureDebRn, Heure_Fin_Activite = ru.HeureFinRn }).ToList();

当我为这两个结果应用 union 时,我得到了这个错误:

CS1929 C# 'List << anonymous type: string Libelle_Activite, DateTime? Activity_Start_Time, DateTime? Time_Fin_Activit >> Queryable.Union << anonymous type: string Libelle_Activite, DateTime? Activity_Start_Time, DateTime? Time_Fin_Activite >> (IQueryable << anonymous type: string Activity_Label, DateTime? Activity_Start_Time, DateTime? Time_Time_Time >>, IEnumerable << type anonymous: string Activity_Label, DateTime? Time_Time_Time >>, IEnumerable << type anonymous: string Activity_Label, DateTime? Time_Time_Activity >>, DateEnime? << anonymous type: string Libelle_Activite, DateTime? Time_Debut_Activite, DateTime? Time_End_Activite

我做错了什么? 如何解决这个问题?

最佳答案

您在 innerJoin 中定义了一个属性 Heure_Fin_Activit,而在 reunion 中它是 Heure_Fin_Activite。您需要确保它们具有相同的名称和类型:

var reunion= (from ru in context.TbReunion select new 
              { Libelle_Activite = ru.IntituleReunion, 
                Heure_Debut_Activite = ru.HeureDebRn, 
                Heure_Fin_Activit = ru.HeureFinRn }).ToList();

关于c# - 无法在 List<anonymous> LINQ 中应用 Union,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59191712/

相关文章:

c# - c# 接口(interface)中的新词

c# - 如果无法安全地用于生产并且无法描述索引之类的东西,为什么我应该首先使用 Entity Framework 代码

c# - 并行化/多线程单例对象方法调用

c# - 具有多个 .net 应用程序的 Web API 2.0 Owin 授权

C# 从对象列表中删除对象

c# - .net calendar - 使整个单元格执行回发(可点击)

c# - 大端还是小端?

c# - 如何使用 LINQ 返回不同的记录

c# - 按多列分组

c# - 带条件 where 子句的 LINQ