不允许在查询中显式构造实体类型''

时间:2011-09-08 17:24:59

标签: c# linq .net-4.0

我收到以下错误

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)

这是我的代码。

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服务。我在调用它时遇到了问题。

5 个答案:

答案 0 :(得分:3)

我使用匿名类型来解决此问题(在关键字“new”之后删除了TableClassName。)

然后我有另一个问题,使用我的TableClassName迭代var,因为var中的内容是匿名的错误。

我遍历var,使用var,然后逐个地将每个var的内容提取到我的对象中,然后将对象添加到我最初预期的列表中。这是我的代码。

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);
}

希望这有助于某人。

答案 1 :(得分:3)

请考虑以下事项:

context.DeferredLoadingEnabled = false; 
tgdd = context.TableClassName.Where(...) 
                             .GroupBy(g => new { g.Date, g.UserID })
                             .AsEnumerable() 
                             .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(); 

通过在Group操作后调用AsEnumerable,我们强制将投影放入客户端的自定义类型,而不是服务器不知道您的自定义类型的服务器。此外,通过在查询上调用ToList,您无需迭代结果并将它们添加到另一个列表中。只需将第一个ToList的结果直接分配给您要返回的对象。

答案 2 :(得分:1)

尝试从LINQ查询中拆分“new TableClassName”部分。

您可能想查看此主题:

http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/1ce25da3-44c6-407d-8395-4c146930004b/

“实体的使用模式是它们是在查询之外创建的,并通过DataContext插入表中,然后通过查询检索,从不由查询创建”

答案 3 :(得分:0)

如果您创建一个没有[Table] -Attribute的新派生类,可能会这样:

public class TableClassNameLocal : TableClassName
{
}

然后,您可以在select中创建“TableClassNameLocal”的实例。将新实例直接转换为“TableClassName”,它将起作用!

result = new List<TableClassName>();
context.DeferredLoadingEnabled = false;
var t = context.TableClassName.Where(...)
                              .GroupBy(g => new { g.Date, g.UserID })
                              .Select(g => (TableClassName)new TableClassNameLocal
                              {
                                    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();

答案 4 :(得分:0)

这对我有用:

  var roles = (from r in roleNames
                                    join rm in DataContext.FMC_RoleMaps on r equals rm.RoleName_old
                                    select new
                                    {
                                        dnnUser.UserID,
                                        rm.RoleID
                                    }
                                           );
                       foreach (var userRole in roles)
                       {
                           var newrole = new UserRole()
                                             {
                                                 RoleID = userRole.RoleID,
                                                 UserID = userRole.UserID
                                             };
                           DataContext.UserRoles.InsertOnSubmit(newrole);
                       }