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的新手。
答案 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();
}