0

我正在返回IQueryable<Customer>另一种方法进行一些查询操作。返回方法如下所示:

return from cust in _dbCustList
               select new Customer
               {
                   CustomerId = cust.Customer_Id,
                   FirstName= cust.First_Name,
                   LastName= cust.Last_Name,
                   DOB= cust.Date_Of_Birth,
                   LoginTime = cust.Login_Time ?? new TimeSpan(0, 0, 0);
               };

在上面的结果中,cust.Login_Time是可为空的属性。

当我尝试查询上述结果时,它会抛出一个错误:

Method 'System.TimeSpan GetTimeSpan(System.Nullable`1[System.TimeSpan])' has no supported translation to SQL.

如何解决这个错误?

4

2 回答 2

0

为什么要使用空检查?

当您删除空检查时,书面查询将被转换为 SQL 查询并将被执行。现在你有了结果,你可以做任何你想做的魔法......

于 2009-10-31T16:02:31.720 回答
0

我会查询一个匿名类型,然后将结果映射到内存中的业务对象:

var q = from cust in _dbCustList
        select new
        {
            cust.Customer_Id,
            cust.First_Name,
            cust.Last_Name,
            cust.Date_Of_Birth,
            cust.Login_Time
        };

return from cust in q.AsEnumerable()
       select new Customer
       {
           CustomerId = cust.Customer_Id,
           FirstName= cust.First_Name,
           LastName= cust.Last_Name,
           DOB= cust.Date_Of_Birth,
           LoginTime = cust.Login_Time ?? TimeSpan.Zero;
       };
于 2009-10-31T22:11:33.433 回答