views:

196

answers:

2

I am returning IQueryable<Customer> to the other method for some querying operations. The return method looks like:

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);
               };

In the above result, cust.Login_Time is nullable property.

When i try to query the above result, it throws an error:

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

How to solve this error?

A: 

Why do you use the null- check?

When you remove the null check the written query gets translated into a SQL query and will be executed. Now you have the result you can do any magic you want...

ollifant
it throws an error, when i dont specify that:Cannot implicitly convert type 'System.TimeSpan?' to 'System.TimeSpan'. An explicit conversion exists (are you missing a cast?)
Prasad
Then should query everything (select cust) or change your Customer class.
ollifant
That property/column is from table(DataContext), and i am assigning it to my application specific customer Class. While assigning it to customer class i m getting that error
Prasad
A: 

I would query into an anonymous type and then map the result to your business object in-memory:

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;
       };
dahlbyk
This one worked for my scenario
Prasad