1

What is happening is when the value, per example, kitefiltered.MarcaId == null (the user left empty), this query check if the value is null. But is not what I want. I just want to check if the value inserted != null (new value), and if its null, jump to the next where clause.

I don´t think the best solution its check before, because are many values to check.

var kitecadastrado = db.KiteCadastrados.Where(f => ((f.MarcaId == kitefiltered.MarcaId) && 
            (f.ModeloId == kitefiltered.ModeloId)&&(f.NumSerie == kitefiltered.NumSerie) &&...

EDIT 1: To clarify my question. I want If kitefiltered.MarcaId != null, TEST (f.MarcaId == kitefiltered.MarcaId) ELSE JUMP TO NEXT CLAUSE && ....

Thanks.

Chris Schmich
  • 29,128
  • 5
  • 77
  • 94
MC Fer
  • 303
  • 1
  • 2
  • 15
  • `if ((f.MarcaId == null || f.Mercaid == kitefiltered.MarcaId) &&` + next clause – John Wu Apr 17 '18 at 01:25
  • i cannot test like this @JohnWu – MC Fer Apr 17 '18 at 01:36
  • "and if its null, jump to the next where clause". I only see 1 where clause in this code. So I don't know what you are trying to accomplish here. – P.Brian.Mackey Apr 17 '18 at 01:46
  • @P.Brian.Mackey If i am writing wrong, please correct me, but for me, these are all my where clauses: (f.MarcaId == kitefiltered.MarcaId) && (f.ModeloId == kitefiltered.ModeloId)&&(f.NumSerie == kitefiltered.NumSerie) ... – MC Fer Apr 17 '18 at 01:57

2 Answers2

1

NULL coalesce can have surprising results in LINQ to entities/SQL. SQL does not treat NULL the same way C# does. != NULL seems to translate well.

If I follow you correctly, the main issue is that you are filtering by NULL in cases where you don't want to be. That can be especially tricky because of the SQL translations I mentioned. I recommend using PredicateBuilder to build up your predicates using Expressions. This works on my machine, but it's tough to test because I don't have your context or EF setup on my machine.

You may need to use AsExpandable() from LINQKit because you are working with EF.

void Main()
{
    var predicate = PredicateBuilder.True<KiteCadastrado>();
    var filter = new KiteFilter();//your kitefiltered object 
    filter.MarcaId = "98272";//fill in your own values here
    filter.NumSerie = "Unit E";

    if(filter.MarcaId != null){
        predicate = predicate.And(p => p.MarcaId == filter.MarcaId);
    }
    if(filter.NumSerie != null){
        predicate = predicate.And(p => p.NumSerie == filter.NumSerie);
    }
    //fill out remaining values
    db.KiteCadastrados.AsExpandable().Where(predicate).Count();//Added AsExpandable()

}

public class KiteFilter
{
    public String MarcaId { get; set; }
    public String NumSerie { get; set; }
}

public static class PredicateBuilder
{
    public static Expression<Func<T, bool>> True<T>() { return f => true; }
    public static Expression<Func<T, bool>> False<T>() { return f => false; }

    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1,
                                                        Expression<Func<T, bool>> expr2)
    {
        var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
        return Expression.Lambda<Func<T, bool>>
              (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
    }

    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,
                                                         Expression<Func<T, bool>> expr2)
    {
        var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
        return Expression.Lambda<Func<T, bool>>
              (Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
    }
}

This way you don't have to include the NULLs in the LINQ expression directly. And you will ultimately end up with a leaner and more flexible expression overall.

P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
  • 1
    Thanks my friend. Worked like a charm... i never had listened about that... I will read the website that you recommend and try to understand better how it works. – MC Fer Apr 17 '18 at 16:28
0

Look into null coalescence for Linq statements. This question here might have the answer you need as I don't have a data set ready to rest with.

This question is the question in question

James Whyte
  • 788
  • 3
  • 14
  • I looked there and didnt find my answer or didnt understand. Look my edit if your answer really answer my question. Thanks – MC Fer Apr 17 '18 at 01:46