9

我创建了一个EF 4 & C#来获取一些数据。我正在使用 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.

任何想法我得到这个错误?

谢谢

4

3 回答 3

5

接到ToList电话后,您希望在 C# 中执行它,而不是在数据库中。用作一种说法,“停止在AsEnumerable数据库中做这些事情,在 C# 中做。”

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

于 2012-04-23T16:24:15.253 回答
5

解决

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

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 中进行转换,而不是直接在这里进行。这解决了我的问题。

希望这对某人有帮助!

于 2012-04-24T19:30:53.617 回答
0

首先,我不会检索整个表,然后像您在此处所做的那样在 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
        })
于 2012-04-23T16:29:10.067 回答