9

データを取得するために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.

このエラーが発生していますか??

ありがとう

4

3 に答える 3

5

解決済み

私には2つの問題があり、それぞれがこのエラーを引き起こしていました。

1NULLABLEエンティティのプロパティに値があるかどうかを確認せずにアクセスしていました。CustomerIDがNULL可能であるとすると、私のクエリは次のようになります

    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プロパティでnull値を確認してください(選択部分の2行目)。

2。また、selectステートメント内で整数を文字列に変換しようとしていました。そこで、ここで直接行うのではなく、HTMLで変換することにしました。これで私の問題は解決しました。

これが誰かに役立つことを願っています!

于 2012-04-24T19:30:53.617 に答える
5

呼び出しに到達したらToList、データベースではなく C# で実行します。AsEnumerable「データベースでこれを行うのはやめて、C# で行います」という言い方として使用します。

最後の の直前にそれを追加して、ToList他のすべてがデータベースで行われるようにします。

于 2012-04-23T16:24:15.253 に答える
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 に答える