C# 无法强制转换类型';System.Int64';输入';System.Object';。LINQ to Entities仅支持转换实体数据模型基元类型
我创建了一个EF4和C#来获取一些数据。我正在使用Linq。其内容如下:C# 无法强制转换类型';System.Int64';输入';System.Object';。LINQ to Entities仅支持转换实体数据模型基元类型,c#,visual-studio-2010,c#-4.0,entity-framework-4,C#,Visual Studio 2010,C# 4.0,Entity Framework 4,我创建了一个EF4和C#来获取一些数据。我正在使用Linq。其内容如下: public List<object> GenerateCallTrackingReport(int startRowIndex, int maximumRows, int createdByID) { var query = from c in this.ObjectContext.CallLogs
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>();
}
你知道我有这个错误吗
谢谢一旦您进入
ToList
呼叫,您希望在C#中执行它,而不是在数据库中。使用AsEnumerable
作为一种说法,“停止在数据库中做这些事情,用C#做。”
在末尾的
ToList
之前添加它,以便对数据库执行所有其他操作。首先,我不会检索整个表,然后像您在这里所做的那样,用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。我访问可为空的实体属性时没有检查它们是否有值。假设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>();
var query=来自this.ObjectContext.CallLogs中的c
选择新的
{
CallDescription=c.CallDescription,
CustomerID=c.CustomerID.HasValue?c.CustomerID.Value:0,
CustomerName=c.CustomerID.HasValue?c.Customer.Name:“
};
如果(最大行数>0)
query=query.Skip(startRowIndex).Take(maximumRows);
返回query.ToList();
所以,在访问任何空值之前,只需通过其HasValue属性检查它(选择部分的第2行)
2。我还试图在select语句中将integer转换为字符串。所以我决定在HTML中进行转换,而不是直接在这里进行转换。这解决了我的问题
希望这对别人有帮助 异常发生在哪一行?@ChristopherRathermel由于Linq To EF的工作方式,无论问题的根源在哪里,异常都可能发生在最后一行,并且大部分是无用的。-1 OPs代码将只执行一个查询。它不会获取所有内容,然后在客户端进行过滤。您的代码执行的查询与他的查询不完全相同,因此它甚至没有任何帮助。这是LINQ的真正功能,它是一个查询,而不是一组数据。这意味着您可以有条件地向查询添加操作,就像OP一样。他甚至将变量命名为“查询”,而不是“数据”,以反映这种区别。最后,OP的问题(您没有解决)完全是因为整个问题都是由DB来解决的,DB不能列出任何东西。至于创建新类型而不是返回对象的部分,我同意这可能是最好的,但这是一个注释,这不是问题的答案。公正的评论,我想我需要进一步研究LINQ,因为我以前的假设似乎不正确-你每天都会学到新东西:)是的:)我得到了这个想法并付诸实施。谢谢你,阿米尔。我的问题也一样。1和2<代码>?将适用于1,而“SqlFunctions.StringConvert”将有助于转换为字符串。谢谢:)
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>();