不允许在查询实体类型'的显式建筑 [英] Explicit construction of entity type '' in query is not allowed

查看:121
本文介绍了不允许在查询实体类型'的显式建筑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误

Explicit construction of entity type '...TableClassName' in query is not allowed.

Server stack trace: 
   at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

下面是我的code。

tgdd = new List<TableClassName>();
    context.DeferredLoadingEnabled = false;
    var t = context.TableClassName.Where(...)
                                  .GroupBy(g => new { g.Date, g.UserID })
                                  .Select(g => new TableClassName
                                  {
                                        PK = 1,
                                        Date = Convert.ToDateTime(g.Key),
                                        Counted = g.Sum(x => x.Counted),
                                        Time = g.Sum(x => x.Time),
                                        MaxHR = g.Max(x => x.MaxHR),
                                        PeakCal = g.Max(x => x.PeakCal),
                                        PowerIndex = (g.Sum(x => x.PowerIndex)),
                                        Target = g.Max(x => x.Target),
                                        RepTotal = g.Sum(x => x.RepTotal),
                                        Lifted = g.Sum(x => x.Lifted),
                                        UserID = Convert.ToInt64(g.Key)
                                   }).ToList();

                        foreach (TableClassName r in t)
                        {
                            tgdd.Add(r);
                        }

我有没有语法错误。这是一个web服务。打电话时,我得到的问题。

I have no syntax errors. This is a web service. I get the issue when calling it.

推荐答案

我用匿名类型来解决这个问题(删除了关键词之后TableClassName新)。

I used anonymous types to solve this issue (removed the TableClassName after the key word "new").

然后,我有另外一个问题,通过VAR迭代,用我的TableClassName(误差由于VAR是匿名的内容)。

Then I had another issue, iterating through the var, using my TableClassName (error due to the contents in var being anonymous).

我迭代通过VAR,使用VAR,然后提取每个变种之一的内容接一个到我的对象,然后添加对象到我的列表作为原本打算。这是我的code为。

I iterated through the var, using var, and then extracting the contents of each var one by one into my object, then adding the object to my list as originally intended. Here's my code for that.

foreach (var v in t)
{
    TableClassName tgData = new TableClassName();
    tgData.PK = v.PK;
    tgData.Date = v.Date;
    tgData.Counted= v.Counted;
    tgData.Time = v.Time;
    tgData.MaxHR = v.MaxHR;
    tgData.PeakCal = v.PeakCal;
    tgData.PowerIndex = v.PowerIndex;
    tgData.Target = v.Target;
    tgData.RepTotal = v.RepTotal;
    tgData.Lifted = v.Lifted;
    tgData.UserID = v.UserID;
    tgdd.Add(tgData);
}

希望这可以帮助别人。

Hope this helps someone.

这篇关于不允许在查询实体类型'的显式建筑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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