EntityFramework Core中的GroupBy和Distinct

时间:2017-03-12 06:02:23

标签: linq entity-framework-core

EF核心剂量不支持GroupBy,我认为Distinct我有同样的问题。

所以,我该如何解决?

我只获得100个第一个元素,然后在结果列表

上调用Distinct()

有更好的解决方案吗?

是否可以将groupby作为扩展方法添加到EFCore?

.Net Core对于报告系统不是一个好主意:/

它是陷阱:(

        query = query.Where(e => e.Goodscode.Contains(filter) || e.GoodsName.Contains(filter));
        return query.Select(e => new GoodsDTO
        {
            Name = e.GoodsName,
            Code = e.Goodscode,
            T3Code = e.T3Code,
            StockId = e.StockId
        }).Take(100).ToList().Distinct(new GoodsDTOComparer()).Take(20);
        //why we do like up: because EF7 dosent support Distinct and GroupBy yet (12-03-2017)
        //microsoft, please don't be OpenSource, because you dont care for your opensource products

1 个答案:

答案 0 :(得分:1)

您可以将Dapper库用于ef core

中不支持的查询

例如

using (IDbConnection dbConnection = new SqlConnection(niniSiteConnectionString))
{

    var sql = @"SELECT Name, Count(*) AS Total FROM Users
                    GROUP BY u.Name
                    HAVING COUNT(*) > 1";

    var result = dbConnection.Query<UserDto>(sql).ToList();
}

public class UserDto
{
    public string Name{get; set;}
    public int Total{get; set;}
}