开发者

Linq query not returning records

开发者 https://www.devze.com 2023-01-28 15:12 出处:网络
public tblCustomerDetail GetCustomerDetailsByID(long ID) { var customer = from c in DataContext.GetTable<tblCustomerDetail>() where c.ID == ID select c;
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.


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

精彩评论

暂无评论...
验证码 换一张
取 消