Linq与nhibernate sql 1=1等效
我正在尝试与LINQ一起搜索NHibernate 我有以下代码:Linq与nhibernate sql 1=1等效,linq,nhibernate,Linq,Nhibernate,我正在尝试与LINQ一起搜索NHibernate 我有以下代码: from d in rep.QueryAll<Document>() where d.Plata != null && d.Contractant != null && d.Stadiu == StadiuDocument.Polita &&am
from d in rep.QueryAll<Document>()
where
d.Plata != null && d.Contractant != null && d.Stadiu == StadiuDocument.Polita
&& (d.NrPolita.Contains(query) ||
d.Contractant.CodUnic.Contains(query) ||
d.Contractant.Denumire.Contains(query) ||
d.Plata.IdTranzactie.Contains(query)) &&
((TipPolita != null) ? (d.Tip == (TipProdus)TipPolita) : (1 == 1)) &&
((StareDocument != null) ? (d.Stare == (StareDocument)StareDocument) : (1 == 1))
select new
{
这与我在SQL中编写的内容几乎相同
Nhibernate源代码中文件“Linq\NHLinqExpression.cs”第33行出现错误
这个错误实际上来自re linq库
一个明显的解决方法是只编写3个if语句,并在每个语句中放入适当的LINQ查询,但这意味着要编写更多的代码
有没有办法让这种查询工作起来,而不复制粘贴整个查询并只修改一点
附言
这是内部异常:
InnerException: System.NullReferenceException
Message=Object reference not set to an instance of an object.
Source=Anonymously Hosted DynamicMethods Assembly
StackTrace:
at lambda_method(Closure
)我将重写以下内容:
((TipPolita != null) ? (d.Tip == (TipProdus)TipPolita) : (1 == 1)) &&
((StareDocument != null) ? (d.Stare == (StareDocument)StareDocument) : (1 == 1))
作为
我不知道它是否能在NHibernate中工作,但它至少更习惯于C#,所以我希望它更有可能得到支持
作为替代,您可以将“1==1”替换为“true”。好吧,找到正确的方法
var date = rep.QueryAll<Document>().Where(d => d.Plata != null && d.Contractant != null && d.Stadiu == StadiuDocument.Polita);
if (!string.IsNullOrEmpty(query))
date = date.Where(d => (d.NrPolita.Contains(query) ||
d.Contractant.CodUnic.Contains(query) ||
d.Contractant.Denumire.Contains(query)));
var date=rep.QueryAll(),其中(d=>d.Plata!=null和d.Contractant!=null和d.statiu==statiudocument.Polita);
如果(!string.IsNullOrEmpty(查询))
日期=日期。其中(d=>(d.NrPolita.Contains)(查询)||
d、 Contractant.CodUnic.Contains(查询)||
d、 包含(查询));
我只是将ifs移到代码中,然后一点一点地构建查询(或者更确切地说是IQueryable)@Para:你尝试过我的替代公式吗?如果你在发帖之前试着用
true
替换1==1
,如果你提到这一点就好了。现在也试过了(我甚至在发帖之前也试过了另一个,这就是我为什么提到beofre的原因),所以现在我两个都试过了,两者都会导致error@Para:请使用您尝试过的所有内容以及错误消息本身的详细信息编辑您的帖子。
InnerException: System.NullReferenceException
Message=Object reference not set to an instance of an object.
Source=Anonymously Hosted DynamicMethods Assembly
StackTrace:
at lambda_method(Closure
((TipPolita != null) ? (d.Tip == (TipProdus)TipPolita) : (1 == 1)) &&
((StareDocument != null) ? (d.Stare == (StareDocument)StareDocument) : (1 == 1))
(TipPolita == null || d.Tip == (TipProdus)TipPolita) &&
(StareDocument == null || d.Stare == (StareDocument)StareDocument)
var date = rep.QueryAll<Document>().Where(d => d.Plata != null && d.Contractant != null && d.Stadiu == StadiuDocument.Polita);
if (!string.IsNullOrEmpty(query))
date = date.Where(d => (d.NrPolita.Contains(query) ||
d.Contractant.CodUnic.Contains(query) ||
d.Contractant.Denumire.Contains(query)));