如何修复linq查询以选择具有分组的ID数?

时间:2016-05-01 19:35:19

标签: entity-framework linq count group-by linq-to-entities

我想创建这个SQL查询到linq:

SELECT  
    COUNT(m.FromUserId) AS Messages,
    m.FromUserId AS UserId
FROM    
    dbo.ChatMessages m
INNER JOIN 
    dbo.ChatMessagesRead mr ON mr.ChatMessageId = m.ChatMessageId
WHERE   
    m.ToUserId = @toUserId
GROUP BY 
    m.FromUserId

我尝试过创建以下linq查询:

var messages = from m in _dbContext.ChatMessages
               join mread in _dbContext.ChatMessagesRead on m.ChatMessageId equals mread.ChatMessageId
               where m.ToUserId == userId
               group m by m.FromUserId into g
               select new
                      {
                         UserId = g.Key,
                         Messages = g.Count()
                      };

var messagesList = messages.ToList();

但这不起作用。

如何修复此linq查询?

我得到了这个例外:

  

表达式类型System.Func 2[Microsoft.Data.Entity.Query.EntityQueryModelVisitor+TransparentIdentifier 2 [Project.BL.ChatMessages.ChatMessages,Project.BL.ChatMessages.ChatMessagesRead],System.Int32]&#39;不能用于#System; System.Func 2[<>f__AnonymousType1 2 [Project.BL.ChatMessages.ChatMessages,Project.BL.ChatMessages.ChatMessagesRead],System.Int32]&#39;方法&#39; System.Collections.Generic.IEnumerable 1[System.Linq.IGrouping 2 [System.Int32,Project.BL.ChatMessages.ChatMessages]] _GroupBy [&lt;&gt; f__AnonymousType1 2,Int32,ChatMessages](System.Collections.Generic.IEnumerable 1 [&lt;&gt; ; f__AnonymousType1 2[Project.BL.ChatMessages.ChatMessages,Project.BL.ChatMessages.ChatMessagesRead]], System.Func 2 [&LT;&GT; f__AnonymousType1 2[Project.BL.ChatMessages.ChatMessages,Project.BL.ChatMessages.ChatMessagesRead],System.Int32], System.Func 2 [&LT;&GT; f__AnonymousType1`2 [Project.BL.ChatMessages.ChatMessages,Project.BL.ChatMessages.ChatMessagesRead],Project.BL。 ChatMessages.ChatMessages])&#39;&#34;

3 个答案:

答案 0 :(得分:1)

我遇到同样的问题,我发现Entity Framework Core bugtracker

上有一个已打开的问题

现在唯一的解决方法似乎是将请求分成两部分。

var filtered = (from m in _dbContext.ChatMessages
                join mread in _dbContext.ChatMessagesRead on m.ChatMessageId equals mread.ChatMessageId
                where m.ToUserId == userId
               select m).ToList();

var messages = from m in filtered
               group m by m.FromUserId into g
               select new
                  {
                     UserId = g.Key,
                     Messages = g.Count()
                  };

答案 1 :(得分:0)

您的代码应该有效。但是,我使用扩展方法创建了另一个版本的查询。

var messages =
    _dbContext
        .ChatMessages
        .Where(message => message.ToUserId == userId)
        .Join(
            _dbContext.ChatMessageRead,
            message => message.ChatMessageId,
            readMessage => readMessage.ChatMessageId,
            (m, mr) => m.FromUserId
        )
        .GroupBy(id => id)
        .Select(group =>
            new
            {
                UserId = group.Key,
                Messages = group.Count()
            }
        );

如果它也抛出相同的异常,你可以试试吗?

答案 2 :(得分:0)

你可以试试这个

var res = ctx.MyTable  // Start with your table
        .GroupBy(r => r.id) / Group by the key of your choice
        .Select( g => new {Id = g.Key, Count = g.Count()}) // Create an anonymous type w/results
        .ToList(); // Convert the results to List