英文:
How do I search in a DataGrid's Items with Multiple dynamic Conditions with LINQ?
问题
我正在使用C# WPF,我有一个DataGrid,通过SQL Server Data填充了我的列表。
我有这个模型:
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; }
}
我的列表:
public List<HAVALEHA_MODEL> LIST_HAVALEHA { get; set; } = new List<HAVALEHA_MODEL>();
我想要在这个DataGrid的项目中进行搜索,但我希望搜索是以动态条件的方式进行的,对于每个填充的字段,都会自动添加条件,最后应用创建的条件,
也就是说,如果用户选择了包括数字、日期等所有字段,并填写了每个字段的条件,最后应用这些条件。
就像这样:
我尝试过什么?
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();
这不是正确的,它不会工作!
很难清楚而简单地解释我需要的确切内容,请理解。
英文:
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 :
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
答案1
得分: 0
在你的 if
循环中,你设置了
LINQ_SEARCH = LIST_HAVALEHA.Where(...);
这意味着每当你进入一个 if
循环(即每当 if
循环的条件为真时),你都会重新定义 LINQ_SEARCH
的值,这是基于在该 if
主体中执行的对 LIST_HAVALEHA
的单一过滤。
据我所见,LIST_HAVALEHA
本身从未改变;因此,实际应用的唯一过滤是在最后一个“激活”的 if
循环中应用到 LINQ_SEARCH
上的过滤。
根据您期望的输出,我建议以下两种方法来解决这个问题:
- 如果你希望在没有应用筛选器时显示所有 havaleha 项目,你可以在应用筛选器之前将
LINQ_SEARCH
设置为等于LIST_HAVALEHA
,然后在你的if
循环中将筛选器应用到LINQ_SEARCH
上:
IEnumerable<HAVALEHA_MODEL> LINQ_SEARCH = LIST_HAVALEHA;
if (!string.IsNullOrEmpty(CUST_N_FLD))
{
LINQ_SEARCH = LINQ_SEARCH.Where(...);
}
if (!string.IsNullOrEmpty(NUMBER_FLD))
{
LINQ_SEARCH = LINQ_SEARCH.Where(...);
}
MY_Data_Ggrid.ItemsSource = LINQ_SEARCH.ToList();
- 如果你更希望在没有应用筛选器时不显示任何 havaleha 项目,你可以让
LINQ_SEARCH
一开始就为空,使用LIST_HAVALEHA
作为应用筛选器时的“备用”,并在填充MY_Data_Ggrid.ItemsSource
时使用一个空列表作为备用:
IEnumerable<HAVALEHA_MODEL> LINQ_SEARCH = null;
if (!string.IsNullOrEmpty(CUST_N_FLD))
{
LINQ_SEARCH = (LINQ_SEARCH ?? LIST_HAVALEHA).Where(...);
}
if (!string.IsNullOrEmpty(NUMBER_FLD))
{
LINQ_SEARCH = (LINQ_SEARCH ?? LIST_HAVALEHA).Where(...);
}
MY_Data_Ggrid.ItemsSource = LINQ_SEARCH?.ToList() ?? new List<HAVALEHA_MODEL>();
我发现第二种方法... 不太好,但至少对于特定用例而言是一个选择。
英文:
In your if
loops, you set
LINQ_SEARCH = LIST_HAVALEHA.Where(...);
, which means that whenever you enter an if
loop (i.e. whenever an if
loop's condition is truthy), you redefine the value of LINQ_SEARCH
based on the single filtering of LIST_HAVALEHA
that is performed in that if
body.
As far as I can see, LIST_HAVALEHA
itself never changes; as a result, the only filtering that is actually applied is the filtering that is applied to LINQ_SEARCH
in the very last 'activated' if
loop.
Depending on your desired output, I'd suggest one of two approaches to get around this:
- If you want to display all havaleha items when no filters are applied, you can set
LINQ_SEARCH
equal toLIST_HAVALEHA
prior to applying the filters, and then applying the filters toLINQ_SEARCH
in yourif
loops:
IEnumerable<HAVALEHA_MODEL> LINQ_SEARCH = LIST_HAVALEHA;
if (!string.IsNullOrEmpty(CUST_N_FLD))
{
LINQ_SEARCH = LINQ_SEARCH.Where(...);
}
if (!string.IsNullOrEmpty(NUMBER_FLD))
{
LINQ_SEARCH = LINQ_SEARCH.Where(...);
}
MY_Data_Ggrid.ItemsSource = LINQ_SEARCH.ToList();
- If you rather want to display no havaleha items when noe filters are applied, you could let
LINQ_SEARCH
be null to begin with, useLIST_HAVALEHA
as a 'backup' when applying filters, and use an empty list as a backup when populatingMY_Data_Ggrid.ItemsSource
:
IEnumerable<HAVALEHA_MODEL> LINQ_SEARCH = null;
if (!string.IsNullOrEmpty(CUST_N_FLD))
{
LINQ_SEARCH = (LINQ_SEARCH ?? LIST_HAVALEHA).Where(...);
}
if (!string.IsNullOrEmpty(NUMBER_FLD))
{
LINQ_SEARCH = (LINQ_SEARCH ?? LIST_HAVALEHA).Where(...);
}
MY_Data_Ggrid.ItemsSource = LINQ_SEARCH?.ToList() ?? new List<HAVALEHA_MODEL>();
I find the second approach ...not so nice, but it is at least an option for that specific use case.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论