开发者

Linq to nhibernate sql 1 = 1 equivalent

开发者 https://www.devze.com 2023-02-05 10:45 出处:网络
I am trying to do a search with LINQ to NHibernate. I have this code: from d in rep.QueryAll<Document>()

I am trying to do a search with LINQ to NHibernate.

I have this code:

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 
                   {

The problem is that I have some select inputs that have general values. Something like this:

    <select id="tippolita" >
        <option value = "-1">Any value</option>
        <option value = "1">Value 1</option>
        <option value = "2">Value 2</option>
        <option value = "3">Value 3</option>
    </select>

So when "Any value" is selected the where statement should be true like I wrote here:

((TipPolita != null) ? (d.Tip == (TipProdus)TipPolita) : (1 == 1)) &&
((StareDocument != null) ? (d.Stare == (StareDocument)StareDocument) : (1 == 1))

This is almost the same as what I would write in SQL.

An error occurs inside the Nhibernate source code at line 33 in the file "Linq\NHLinqExpression.cs"

_expression = PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(expression);

This error actually comes from the re-linq library.

One obvious workaround is to just write 3 if statements and put the appropriat开发者_C百科e LINQ queries in each of them but that means writing a lot more code.

Is there any way to make this kind of query work without copy-pasting the entire query and modifying just a little of it?

P.S.

This is the inner exception:

  InnerException: System.NullReferenceException
       Message=Object reference not set to an instance of an object.
       Source=Anonymously Hosted DynamicMethods Assembly
       StackTrace:
            at lambda_method(Closure 

)


I would rewrite this:

((TipPolita != null) ? (d.Tip == (TipProdus)TipPolita) : (1 == 1)) &&
((StareDocument != null) ? (d.Stare == (StareDocument)StareDocument) : (1 == 1))

as

(TipPolita == null || d.Tip == (TipProdus)TipPolita) &&
(StareDocument == null || d.Stare == (StareDocument)StareDocument)

I don't know whether it'll work in NHibernate or not, but it's more idiomatic C# at least, so I would expect that it's more likely to be supported.

As an alternative, you could just replace "1 == 1" with "true".


Well, figured out how to do this the proper way

            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)));

I just move the ifs into the code and build the query(or rather the IQueryable) bit by bit

0

精彩评论

暂无评论...
验证码 换一张
取 消