【发布时间】:2015-04-10 16:39:04
【问题描述】:
我想在方法中执行 LINQ where 子句。
例如:
using (BP_TTOKEntities db = new BP_TTOKEntities(_dto.IdTenant))
{
var res = db.doc003fornitura
if (fornitura.Numero != null) //Filtro numero
{
if (!fornitura.Numero.LBoundIsNull) res = res.Where(x => x.fornitura_nro >= fornitura.Numero.LBound);
if (!fornitura.Numero.UBoundIsNull) res = res.Where(x => x.fornitura_nro <= fornitura.Numero.UBound);
}
}
我会替换:
if (!fornitura.Numero.LBoundIsNull) res = res.Where(x => x.fornitura_nro >= fornitura.Numero.LBound);
if (!fornitura.Numero.UBoundIsNull) res = res.Where(x => x.fornitura_nro <= fornitura.Numero.UBound);
类似这样的:
res = fornitura.Numero.Where<doc003fornitura>(x.fornitura_nro);
有可能吗?如何制作方法?
谢谢路易吉。
【问题讨论】:
-
写单行字往往不是解决生活问题的真正方法。对正确工作的有序代码感到满意。
-
我们不知道任何所涉及的类型也无济于事。看起来
fornitura.Numero是一个单一的值,所以在它上面调用Where是没有意义的。打电话给db.doc003fornitura.Where(...)会更有意义。如果您能提供一个简短但完整的示例,最好使用更有意义(和传统)的名称,这将很有帮助。 -
另外,您可能希望使用 if.. else if.. 模式而不是 if.. if.. 模式,因为 fornitura.Numero 将始终只有一个值,然后不会如果检查,则不会额外
-
bit ,你说的不对。第一个 if 执行 >= 检查,第二个
标签: c# linq entity-framework linq-to-entities