【发布时间】:2010-03-18 10:34:27
【问题描述】:
我使用 LINQ-SQL 作为我的 DAL,然后我有一个名为 DB 的项目作为我的 BLL。然后各种应用程序访问 BLL 以从 SQL 数据库读取/写入数据。
我的 BLL 中有一张特定表的这些方法:
public IEnumerable<SystemSalesTaxList> Get_SystemSalesTaxList()
{
return from s in db.SystemSalesTaxLists
select s;
}
public SystemSalesTaxList Get_SystemSalesTaxList(string strSalesTaxID)
{
return Get_SystemSalesTaxList().Where(s => s.SalesTaxID == strSalesTaxID).FirstOrDefault();
}
public SystemSalesTaxList Get_SystemSalesTaxListByZipCode(string strZipCode)
{
return Get_SystemSalesTaxList().Where(s => s.ZipCode == strZipCode).FirstOrDefault();
}
我认为一切都非常简单。
Get_SystemSalesTaxListByZipCode 始终返回空值,即使该表中存在邮政编码。
如果我这样写方法,它会返回我想要的行:
public SystemSalesTaxList Get_SystemSalesTaxListByZipCode(string strZipCode)
{
var salesTax = from s in db.SystemSalesTaxLists
where s.ZipCode == strZipCode
select s;
return salesTax.FirstOrDefault();
}
为什么其他方法返回的结果不一样,因为查询应该是相同的?
请注意,当我给它一个有效的SalesTaxID 时,重载的Get_SystemSalesTaxList(string strSalesTaxID) 返回一个记录就好了。
有没有更有效的方法来编写这些“帮助”类型的类?
谢谢!
【问题讨论】:
-
愚蠢的问题 - 你确定你将邮政编码传递给
Get_SystemSalesTaxListByZipCode吗? -
问题出在
IEnumerable<T>- 但我只是大声说“这可能是抱怨匈牙利符号的方式”;-p