How do I search in a DataGrid's Items with Multiple dynamic Conditions with LINQ?

Mojtaba_Hakim 321 Reputation points
2023-02-08T18:47:29.5+00:00

I'm using C# WPF and I have DataGrid that fill with MY LIST via SQL Server Data

I have this model :

public class HAVALEHA_MODEL
        {
            public double? NUMBER { get; set; }
            public string NAME { get; set; }
            public string CUST_NO { get; set; }
            public long? DATE_N { get; set; }
            public string TAH { get; set; }
            public string MOLAH { get; set; }
            public string SHARAYET { get; set; }
            public int? ANBAR { get; set; }
            public double? FNUMCO { get; set; }
            public double? MAS { get; set; }
        }

my list: public List<HAVALEHA_MODEL> LIST_HAVALEHA { get; set; } = new List<HAVALEHA_MODEL>();

I want to do a search in this DataGrid's Items, but I want the search to be done in such a way that a dynamic condition is automatically added for each field that is filled and finally the created condition is applied,

that is, if the user selects all the fields including the number, Fill in the number, date, etc., and make a condition for each one that is filled in, and finally apply it.

like this : 12333

What have I tried ? :

     IEnumerable<HAVALEHA_MODEL> LINQ_SEARCH = null;

            if (!string.IsNullOrEmpty(CUST_N_FLD))
            {
                LINQ_SEARCH = LIST_HAVALEHA.Where(x => !(x.NAME is null) && x.NAME.ToLowerInvariant().Contains(CUST_N_FLD.ToLowerInvariant()));
            }
            if (!string.IsNullOrEmpty(NUMBER_FLD))
            {
                LINQ_SEARCH = LIST_HAVALEHA.Where(x => !(x.NUMBER is null) && x.NAME.ToLowerInvariant().Contains(NUMBER_FLD.Text.Trim().ToLowerInvariant()));
            }
            
            MY_Data_Ggrid.ItemsSource = LINQ_SEARCH.ToList();

that is not correct and it won't work!

It is difficult to clearly and simply explain what I need exactly, please understand

please give me some advice

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,827 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,308 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 56,951 Reputation points
    2023-02-08T18:59:35.6333333+00:00

    You are on the right path. You need to start with the full list of items and then conditionally add a where statement for new conditions. Part of the issue you're having is that you keep adding the where clause to the original list, not the query you're building up.

    //Start with no filters
    IEnumerable<HAVALEHA_MODEL> query = LIST_HAVALEHA;
    
    if (!string.IsNullOrEmpty(CUST_N_FLD))
       query = query.Where(x => String.Equals(x.NAME, CUST_NFLD, StringComparison.InvariantCultureIgnoreCase));
    
    if (!string.IsNullOrEmpty(NUMBER_FLD))
       query = query.Where(x => String.Equals(x.Number, NUMBER_FLD, StringComparison.InvariantCultureIgnoreCase);
                
    MY_Data_Ggrid.ItemsSource = query.ToList();
    
    2 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Khageshwar Joshi 0 Reputation points
    2023-02-10T10:48:54.28+00:00

    img

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.