无法将类型“System.Int64”转换为类型“System.Object”.LINQ to 实体仅支持强制转换实体数

本文关键字:转换 实体 System 类型 支持 to Int64 Object LINQ | 更新日期: 2023-09-27 18:32:04

我创建了一个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.

任何想法是我收到此错误吗?

谢谢

无法将类型“System.Int64”转换为类型“System.Object”.LINQ to 实体仅支持强制转换实体数

进入ToList调用后,您希望在 C# 中执行它,而不是在数据库中执行它。 使用AsEnumerable作为说法,"停止在数据库中做这些事情,用 C# 做。

在末尾的ToList之前添加它,以便其他所有内容都在数据库上完成。

已解决

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

1. 我正在访问 NULLABLE 实体属性,而没有检查它们是否有值。 假设客户 ID可为空的,所以我的查询变成了这样!

    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.我还尝试在选择语句中将整数转换为字符串。所以我只是决定在 HTML 中进行转换,而不是直接在这里进行。这解决了我的问题。

希望这对某人有所帮助!

首先,我不会检索整个表,然后像您在此处所做的那样在 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
        })