【问题标题】:Unable to cast the type 'System.Int64' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types无法将类型“System.Int64”转换为类型“System.Object”。 LINQ to Entities 仅支持转换实体数据模型基元类型
【发布时间】:2012-04-23 16:18:14
【问题描述】:

我创建了一个 EF 4C# 来获取一些数据。我正在使用 Linq。如下:

    public List<object> GenerateCallTrackingReport(int startRowIndex, int maximumRows, int createdByID)
    {
        var query = from c in this.ObjectContext.CallLogs                        
                    select new
                    {
                        CallLogID = c.CallLogID,
                        DomainName = c.CallDomain.FullName,
                        CreatedByID = c.CreatedByID,
                        CreatedBy = c.CreatedByUser.FirstName + " " + c.CreatedByUser.LastAccessIPN,
                        CalledOn = c.CallDate,
                        IssueResolutionTime = c.IssueResolutionTime,                            
                        CallType = c.CallType.FullName,
                        CallDescription = c.CallDescription,
                        CustomerName = (c.CustomerID > 0 ? c.Customer.FirstName + " " + c.Customer.LastAccessIPN : c.TempCaller.FirstName + " " + c.TempCaller.LastName),
                        CustomerEmail = (c.CustomerID > 0 ? c.Customer.Email : string.Empty),
                        CustomerResponse = c.Response.FullName,
                        IsPending = c.IsPending,
                        NeedFurtherContact = c.NeedFurtherContact
                    };

        if (createdByID > 0)
            query = query.Where(c => c.CreatedByID == createdByID);

        if (maximumRows > 0)
            query = query.Skip(startRowIndex).Take(maximumRows);

        return query.ToList<object>();

    }

这会导致以下错误:

Unable to cast the type 'System.Int64' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types.

你知道我收到这个错误了吗??

谢谢

【问题讨论】:

  • 异常在哪一行?
  • @ChristopherRathermel 由于 Linq-To-EF 的工作原理,无论问题的根源在哪里,异常都可能发生在最后一行,并且几乎无用。

标签: c# visual-studio-2010 c#-4.0 entity-framework-4


【解决方案1】:

一旦您调用ToList,您希望在 C# 中执行它,而不是在数据库中。使用AsEnumerable 作为一种说法,“停止在数据库中做这些事情,在 C# 中做。”

在最后的ToList 之前添加它,以便在数据库上完成其他所有操作。

【讨论】:

    【解决方案2】:

    已解决

    我有两个问题,每个都会导致这个错误:

    1。我在访问 NULLABLE 实体属性时没有检查它们是否有价值。假设 CustomerIDNULLABLE,所以我的查询变成了这样!

        var query = from c in this.ObjectContext.CallLogs                        
                    select new
                    {
                        CallDescription = c.CallDescription,
                        CustomerID = c.CustomerID.HasValue ? c.CustomerID.Value : 0,
                        CustomerName = c.CustomerID.HasValue ? c.Customer.Name : ""
                    };
    
        if (maximumRows > 0)
            query = query.Skip(startRowIndex).Take(maximumRows);
    
        return query.ToList<object>();
    

    因此,只需在访问之前通过其 HasValue 属性检查任何空值(选择部分的第二行)。

    2。我还试图在 select 语句中将整数转换为字符串。所以我只是决定在 HTML 中进行转换,而不是直接在这里进行。这解决了我的问题。

    希望这对某人有所帮助!

    【讨论】:

    • 是的 :) 我得到了这个想法并实施了。谢谢阿米尔。 :)。我的问题也是一样的。 1 和 2。? 将适用于 1,而 'SqlFunctions.StringConvert' 将有助于转换为字符串。谢谢:)
    【解决方案3】:

    首先,我不会检索整个表,然后在 C# 中对完整数据集进行查询,就像您在此处所做的那样。像这样将 linq 链接到实体方法会使其更快 - 当你获得大量数据集时:

    this.ObjectContext.CallLogs
        .Where(c => c.CreatedByID == createdByID)
        .Skip(startRowIndex)
        .Take(maximumRows)
        .Select(new
            {
                            CallLogID = c.CallLogID,
                            DomainName = c.CallDomain.FullName,
                            CreatedByID = c.CreatedByID,
                            CreatedBy = c.CreatedByUser.FirstName + " " + c.CreatedByUser.LastAccessIPN,
                            CalledOn = c.CallDate,
                            IssueResolutionTime = c.IssueResolutionTime,                            
                            CallType = c.CallType.FullName,
                            CallDescription = c.CallDescription,
                            CustomerName = (c.CustomerID > 0 ? c.Customer.FirstName + " " + c.Customer.LastAccessIPN : c.TempCaller.FirstName + " " + c.TempCaller.LastName),
                            CustomerEmail = (c.CustomerID > 0 ? c.Customer.Email : string.Empty),
                            CustomerResponse = c.Response.FullName,
                            IsPending = c.IsPending,
                            NeedFurtherContact = c.NeedFurtherContact
            })
        .ToList();
    

    其次,我不知道确切的问题,但是创建这样的动态对象列表并不是一个好主意。创建一个 CallLogModel 类,其中包含您要放入对象中的属性,如下所示:

    .Select(new CallLogModel
            {
                            CallLogID = c.CallLogID,
                            DomainName = c.CallDomain.FullName,
                            CreatedByID = c.CreatedByID,
                            CreatedBy = c.CreatedByUser.FirstName + " " + c.CreatedByUser.LastAccessIPN,
                            CalledOn = c.CallDate,
                            IssueResolutionTime = c.IssueResolutionTime,                            
                            CallType = c.CallType.FullName,
                            CallDescription = c.CallDescription,
                            CustomerName = (c.CustomerID > 0 ? c.Customer.FirstName + " " + c.Customer.LastAccessIPN : c.TempCaller.FirstName + " " + c.TempCaller.LastName),
                            CustomerEmail = (c.CustomerID > 0 ? c.Customer.Email : string.Empty),
                            CustomerResponse = c.Response.FullName,
                            IsPending = c.IsPending,
                            NeedFurtherContact = c.NeedFurtherContact
            })
    

    【讨论】:

    • -1 OPs 代码将只执行一个查询。它不会获取所有内容,然后在客户端进行过滤。您的代码执行的查询与他的查询不完全相同,因此它甚至没有任何帮助。这是 LINQ 的真正力量,它是一个查询,不是一组数据。这意味着您可以有条件地向查询添加操作,就像 OP 一样。他甚至将变量命名为“查询”而不是“数据”以反映这种区别。最后,OP的问题(不是你解决的)完全是因为整个事情都试图由DB解决,它不能ToList任何东西。
    • 关于创建一个新类型而不是返回object,我同意这可能是最好的,但这是一个评论,而不是问题的答案。
    • 足够公平的评论,我想我需要更多地研究 LINQ,因为我之前的假设似乎不正确 - 你每天都会学到新东西 :)
    猜你喜欢
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多