1

How i can do LINQ Querie.

List<ApplicationUser> ListaUsuarios = AccountControl.UserManager.Users.Where(i => i.Nombre == Filter.Nombre).ToList();

With many Filters when some attributs can coming with Null Value. For example: My FilterGeneric Class have many Attribute accept Nulls.

public class FilterGeneric
{
    public DataCollectionType.FiltrosPdf Tipo { get; set; }
    public string PdfTitle { get; set; }
    public string pdfDescription { get; set; }
    public string Nombre { get; set; }
    public string Cargo { get; set; }
    public string Iniciales { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
    public bool? Enabled { get; set; }
    public DateTime? Date_since { get; set; }
    public DateTime? Date_to { get; set; }
    public string RoleName { get; set; }
    public string Id_Sucursal { get; set; }
    public string RUC { get; set; }
    public string Direccion { get; set; }
    public int? Direccion_Nro { get; set; }
    public string Telefono { get; set; }
    public int? Id_Localidad { get; set; }


}

Is that Possible? Thanks all for listening.

UPDATE:

I test with Answers

1#:

if (Filter.Nombre != null)
        {
            query = query.Where(i => i.Nombre == Filter.Nombre);
        }

2#:

List<ApplicationUser> ListaUsuarios = AccountControl.UserManager.Users.Where
        (x =>
           (x.Nombre == Filter.Nombre || string.IsNullOrEmpty(Filter.Nombre)) &&
            (x.Nombre == Filter.Cargo || string.IsNullOrEmpty(Filter.Cargo)) &&
            (x.Nombre == Filter.Iniciales || string.IsNullOrEmpty(Filter.Iniciales)) &&
            (x.Nombre == Filter.UserName || string.IsNullOrEmpty(Filter.UserName))


        ).ToList();

I get this Error:

enter image description here

3 Answers3

2

You need to add all the filters one by one in the following manner:

List<ApplicationUser> ListaUsuarios = 
     AccountControl.UserManager.Users
     .Where(
     i => 
     (i.Nombre == Filter.Nombre || string.IsNullOrEmpty(Filter.Nombre))
     &&
     (i.Cargo == Filter.Cargo || string.IsNullOrEmpty(Filter.Cargo))
     ).ToList();

This one is telling you, that if Filter.Nombre is null/empty just ignore it. Same case for Filter.Cargo and so on.

For nullable int

(Filter.Direccion_Nro == null || i.Direccion_Nro == Filter.Direccion_Nro.Value)
Sunil
  • 3,404
  • 10
  • 23
  • 31
  • Hello Sunil, thanks for you answer. I update post with an Error, i test your option – Maximiliano Cesán Dec 11 '17 at 15:30
  • Can you put a debug break point and check which of the object is null. It could be one of these `AccountControl`, `AccountControl.UserManager`, `AccountControl.UserManager.Users` or `Filter` object. If possible post results here – Sunil Dec 11 '17 at 16:04
2

As the query gets materialized (means executed) when you call something that materializes it (.ToList(), ToArray() or a foreach for example), you can just chain them conditionally:

IEnumerable<ApplicationUser> query = AccountControl.UserManager.Users;

if(Filter.Nombre != null)
{
    query = query.Where(i => i.Nombre == Filter.Nombre);
}

List<ApplicationUser> ListaUsuarios = query.ToList();
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • Hello nvoigt, thanks for you answer. I update post with an Error, i test your option – Maximiliano Cesán Dec 11 '17 at 15:30
  • @MaximilianoCesán That error has nothing to do with *either* answer. Please refer to [the canonical answer for your problem](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it). – nvoigt Dec 11 '17 at 15:32
  • That bro? i change Image Exception – Maximiliano Cesán Dec 11 '17 at 15:36
  • By defining query as `IEnumerable`, you are going to have the following filters applied client side (LINQ to Objects) rather than server side. Change this explicitly use the `IQueryable` definition instead. – Jim Wooley Dec 11 '17 at 16:53
  • @JimWooley This is not tagged as anything like linq-to-sql or entity framework. For all I know this is an `IEnumerable` like a `List` or something. – nvoigt Dec 11 '17 at 16:56
1

I think, you can perform it by using Reflection dynamically;

        //Determine the not null properties of Filter object
        var notNullProperties = Filter.GetType().GetProperties().Where(x => x.GetValue(Filter) != null).ToList();

        //Perform where clause for not null properties of Filter
        if (notNullProperties.Count > 0)
        {
            var ListaUsuarios = AccountControl.UserManager.Users.Where(x =>
                notNullProperties.All(n => n.GetValue(Filter) == x.GetType().GetProperty(n.Name).GetValue(x))).ToList();
        }
lucky
  • 12,734
  • 4
  • 24
  • 46