Linq联合两个表,其中有一个额外的列

时间:2016-01-16 00:48:28

标签: c# linq union

我使用Union来合并两个表auditmessage。它们都具有所有相同的列名,除了audit有一个名为deleteDate的额外列。我尝试使用Union合并两个表,如果deleteDate不存在,则将DeleteDate方法设置为null。我评论了我想要上班的东西。当我取消注释代码时,我收到一个错误。有工作吗?这是我的代码

    var audit = from a in _Audit.GetAll()
                       .Where(a => a.PInt == pInt && a.CreateDate < startDate && (endDate == null || endDate > a.CreateDate))
                       select new
                       {
                           a.MInt,
                           a.MId,
                           a.Desc,
                           a.PString,
                           a.PType,
                           a.MType,
                           a.CreateDate,
                           //    a.DeleteDate,
                           a.PInt
                       };

    var message = from a in _Message.GetAll()
           .Where(a => a.PartnerInt == partnerInt && a.CreateDate < startDate && (endDate == null || endDate > a.CreateDate))
                  select new
                  {
                      a.MInt,
                      a.MId,
                      a.Desc,
                      a.PString,
                      a.PType,
                      a.MType,
                      a.CDate,
                      a.PInt
                  };

    var test = from a in audit.Union(message)
               select new AuditMessagesGroup
               {
                   MInt = a.Int,
                   MId = a.Id,
                   Desc = a.Desc,
                   PString = a.PString,
                   PType = a.PayloadType,
                   MessageType = a.MType,
                   CreateDate = a.CreateDate,
         //          DeleteDate = a.DeleteDate != null ? a.DeleteDate : null,
                   PInt = a.PInt

               };

这是错误

    Error   CS1929  'IQueryable<<anonymous type: int MInt, Guid 

MId, string Desc, string PString, string PType, 

MTypes MType, DateTime CreateDate, DateTime DeleteDate, int? 

PInt>>' does not contain a definition for 'Union' and the best 

extension method overload 'ParallelEnumerable.Union<<anonymous type: int 

MInt, Guid MId, string Desc, string PString, string 

PType, MTypes MType, DateTime CreateDate, int? PInt>>

(ParallelQuery<<anonymous type: int MInt, Guid MId, string 

Desc, string PString, string PType, MTypes 

MType, DateTime CreateDate, int? PInt>>, IEnumerable<<anonymous 

type: int MInt, Guid MId, string Desc, string 

PString, string PType, MTypes MType, DateTime 

CreateDate, int? PInt>>)' requires a receiver of type 

'ParallelQuery<<anonymous type: int MInt, Guid MId, string 

Desc, string PString, string PType, MTypes 

MType, DateTime CreateDate, int? PInt>>'    

2 个答案:

答案 0 :(得分:1)

要使.Union(...)工作,匿名类型必须完全相同 - 并且要求它具有完全相同的字段数,具有完全相同的类型,并且完全相同名。

所以,从您的查询中,我认为您需要这个:

var audit =
    from a in _Audit
        .GetAll()
        .Where(a => a.PInt == pInt)
        .Where(a => a.CreateDate < startDate)
        .Where(a => endDate == null || endDate > a.CreateDate)
    select new
    {
        a.MInt,
        a.MId,
        a.Desc,
        a.PString,
        a.PType,
        a.MType,
        a.CreateDate,
        a.DeleteDate,
        a.PInt
    };

var message =
    from a in _Message
        .GetAll()
        .Where(a => a.PartnerInt == partnerInt)
        .Where(a => a.CreateDate < startDate)
        .Where(a => endDate == null || endDate > a.CreateDate)
    select new
    {
        a.MInt,
        a.MId,
        a.Desc,
        a.PString,
        a.PType,
        a.MType,
        CreateDate = a.CDate,
        DeleteDate = (DateTime?)null,
        a.PInt
    };

我无法确定每张桌子的类型是什么,但希望非常接近。

您可能需要使用.ToArray()然后使用新的.Select(...)将记录拉入内存以使字段类型对齐,但​​假设它们已经相同,则它应该是工作得很好。

答案 1 :(得分:0)

不,你不能。 union的基本标准是列数必须与b / w表匹配。您可以生成虚拟列以绕过。像

考虑a.DeleteDate DateTime audit messagevar message = from a in _Message.GetAll() .Where(a => a.PartnerInt == partnerInt && a.CreateDate < startDate && (endDate == null || endDate > a.CreateDate)) select new { a.MInt, a.MId, a.Desc, a.PString, a.PType, a.MType, a.CDate, DateTime.Now, // a dummy column a.PInt }; 更改为

UNION

然后您可以执行class ListApp extends Component { constructor(props){ super(props); this.state = { loaded: false }; } //, <--- no comma! ... ... ... }