Linq查询不返回记录

时间:2010-11-30 04:50:13

标签: .net linq linq-to-sql

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()中包含记录,在根据ID过滤后,客户变量中没有记录,尽管在返回的表中存在我正在搜索的ID的记录。

请帮忙。我是LINQ的新手。

1 个答案:

答案 0 :(得分:3)

您的变量客户的类型为IEnumerable<tblCustomerDetail>,因此当您使用as运算符进行投射时,结果将为null,因为类型不兼容。

请改为尝试:

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