【解決方法】LINQ で複数の動的条件を持つデータグリッドのアイテムを検索するにはどうすればよいですか?


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

I have this model :

 <pre lang="C#">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; }
        }

私のリスト:

C#
public List<HAVALEHA_MODEL> LIST_HAVALEHA { get; set; } = new List<HAVALEHA_MODEL>();

この DataGrid の Items で検索を実行したいのですが、入力されたフィールドごとに動的な条件が自動的に追加され、最後に作成された条件が適用されるように検索を実行したいのですが、

つまり、ユーザーが数字を含むすべてのフィールドを選択した場合、数字、日付などを入力し、入力された各フィールドに対して条件を作成し、最後に適用します。

私が試したこと:

What have I tried ? :

      <pre lang="C#">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();

それは正しくなく、機能しません。

アドバイスをください

解決策 1

WinForms を使用している場合、Linq は必要ありません。 bindingSource.Filter[^] 次のような検索クエリを渡します。

FirstName like '*30*' and Age > 40 and age < 50

同様の質問に使用されるサンプルコードは次のとおりです…

1. Form1.Designer.cs

C#
partial class Form1
{
    private System.ComponentModel.IContainer components = null;

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    private void InitializeComponent()
    {
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.txtSearch = new System.Windows.Forms.TextBox();
        this.butSearch = new System.Windows.Forms.Button();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        this.SuspendLayout();
        // 
        // dataGridView1
        // 
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Top;
        this.dataGridView1.Location = new System.Drawing.Point(0, 0);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.RowHeadersWidth = 62;
        this.dataGridView1.RowTemplate.Height = 33;
        this.dataGridView1.Size = new System.Drawing.Size(800, 419);
        this.dataGridView1.TabIndex = 0;
        // 
        // txtSearch
        // 
        this.txtSearch.Dock = System.Windows.Forms.DockStyle.Left;
        this.txtSearch.Location = new System.Drawing.Point(0, 419);
        this.txtSearch.Name = "txtSearch";
        this.txtSearch.Size = new System.Drawing.Size(682, 31);
        this.txtSearch.TabIndex = 2;
        // 
        // butSearch
        // 
        this.butSearch.Dock = System.Windows.Forms.DockStyle.Right;
        this.butSearch.Location = new System.Drawing.Point(688, 419);
        this.butSearch.Name = "butSearch";
        this.butSearch.Size = new System.Drawing.Size(112, 31);
        this.butSearch.TabIndex = 3;
        this.butSearch.Text = "button1";
        this.butSearch.UseVisualStyleBackColor = true;
        this.butSearch.Click += new System.EventHandler(this.butSearch_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(800, 450);
        this.Controls.Add(this.butSearch);
        this.Controls.Add(this.txtSearch);
        this.Controls.Add(this.dataGridView1);
        this.Name = "Form1";
        this.Text = "Form1";
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private DataGridView dataGridView1;
    private TextBox txtSearch;
    private Button butSearch;
}

2. 分離コード:

C#
public partial class Form1 : Form
{
    DataSet dataSet = new();
    BindingSource bindingSource = new();

    private Random random = new();

    public Form1()
    {
        InitializeComponent();
        InitData();
    }

    private void InitData()
    {
        DataTable dataTable = new();

        dataTable.Columns.Add(new DataColumn("FirstName"));
        dataTable.Columns.Add(new DataColumn("LastName"));
        dataTable.Columns.Add(new DataColumn("Age"));

        for (int i = 0; i < 35000; i++)
        {
            var row = dataTable.NewRow();
            row["FirstName"] = $"FirstName{i:000#}";
            row["LastName"] = $"LastName{i:000#}";
            row["Age"] = random.Next(20, 60);
            dataTable.Rows.Add(row);
        }

        dataSet.Tables.Add(dataTable);

        DataView view1 = new DataView(dataSet.Tables[0]);
        bindingSource.DataSource = view1;

        dataGridView1.AutoGenerateColumns = true;
        dataGridView1.DataSource = bindingSource;
    }

    private void butSearch_Click(object sender, EventArgs e)
        => bindingSource.Filter = txtSearch.Text;
}

コメント

タイトルとURLをコピーしました