只有无参构造函数初始化和LINQ中支持到实体消息 [英] Only parameterless constructors and initializers are supported in LINQ to Entities message

查看:129
本文介绍了只有无参构造函数初始化和LINQ中支持到实体消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从EF模型返回数据的方法。

I have a method that returns data from an EF model.

我收到了上述消息,但我不能wotk我们如何绕过这个问题。

I'm getting the above message, but I can't wotk our how to circumvent the problem.

    public static IEnumerable<FundedCount> GetFundedCount()
    {
        var today = DateTime.Now;
        var daysInMonth = DateTime.DaysInMonth(today.Year, today.Month);

        var day1 = DateTime.Now.AddDays(-1);
        var day31 = DateTime.Now.AddDays(-31);

        using (var uow = new UnitOfWork(ConnectionString.PaydayLenders))
        {
            var r = new Repository<MatchHistory>(uow.Context);

            return r.Find()
                .Where(x =>
                    x.AppliedOn >= day1 && x.AppliedOn <= day31 &&
                    x.ResultTypeId == (int)MatchResultType.Accepted)
                .GroupBy(x => new { x.BuyerId, x.AppliedOn })
                .Select(x => new FundedCount(
                    x.Key.BuyerId,
                    x.Count() / 30 * daysInMonth))
                .ToList();
        }
    }

FundedCount不是一个EF enity,MatchHistory是,所以不明白为什么它是抱怨。

FundedCount is not an EF enity, MatchHistory is, so can't understand why it is complaining.

所有建议AP preciated。

All advice appreciated.

推荐答案

有抱怨的原因是因为它不知道如何翻译你的选择()进入一个SQL前pression。如果你需要做一个数据转换到该POCO的不是一个实体,你应该首先从EF相关的数据,然后将其转化为POCO。

The reason it is complaining is because it doesn't know how to translate your Select() into a SQL expression. If you need to do a data transformation to a POCO that is not an entity, you should first get the relevant data from EF and then transform it to the POCO.

在你的情况下,它应该是为调用简单了ToList()较早

In your case it should be as simple as calling ToList() earlier:

return r.Find()
        .Where(x => x.AppliedOn >= day1 && x.AppliedOn <= day31 &&
                    x.ResultTypeId == (int)MatchResultType.Accepted)
        .GroupBy(x => new { x.BuyerId, x.AppliedOn })
        .ToList() // this causes the query to execute
        .Select(x => new FundedCount(x.Key.BuyerId, x.Count() / 30 * daysInMonth));

小心这一点,虽然,并确保你限制由了ToList()尽可能返回的数据集的大小,让你再不是要整个表加载到内存中。

Be careful with this, though, and make sure that you're limiting the size of the data set returned by ToList() as much as possible so that you're not trying to load an entire table into memory.

这篇关于只有无参构造函数初始化和LINQ中支持到实体消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆