teenup
teenup

Reputation: 7667

Linq query not returning records

public tblCustomerDetail GetCustomerDetailsByID(long ID)
        {
            var customer = from c in DataContext.GetTable<tblCustomerDetail>() where c.ID == ID select c;
            return customer as tblCustomerDetail;
        }

DataContext.GetTable() has records in it and after filtering based on ID, there are no records in customer variable, although the record with the ID for which I am searching exists in the returned table.

Please help. I am new to LINQ.

Upvotes: 1

Views: 321

Answers (1)

Quick Joe Smith
Quick Joe Smith

Reputation: 8222

Your variable customer will be of type IEnumerable<tblCustomerDetail> so when you cast it with the as operator, the result will be null because the types are incompatible.

Try this instead:

public tblCustomerDetail GetCustomerDetailsByID(long ID)
{
    var customer = from c in DataContext.GetTable<tblCustomerDetail>() where c.ID == ID select c;
    return customer.First();
}

Upvotes: 3

Related Questions