简体   繁体   English

C#数据表过滤器

[英]C# Datatable filter

I have 2 columns "PriceList" and "Group" in a DataTable. 我在DataTable中有2列“ PriceList”和“ Group”。

PriceList contains, List1, List2, List3

Group contains, Group1, Group2, Group 3 组包含组1,组2,组3

var checkValues1 = new string[] { "List1", "List2" };

var checkValues2 = new string[] { "Group" };

This works 这有效

                dt.AsEnumerable().ToList().ForEach(x =>
                {
                    if (checkValues1.Contains(x["PriceList"]))
                    {
                        newDT.ImportRow(x); //Copy
                        DT.Rows.Remove(x); //Remove
                    }
                });

But I cant get this to work 但是我不能让它工作

                dt.AsEnumerable().ToList().ForEach(x =>
                {
                    if (checkValues2.Contains(x["Group"]))
                    {
                        newDT.ImportRow(x); //Copy
                        DT.Rows.Remove(x); //Remove
                    }
                });

Seems it need to be an exact match so it will only work for when group names is exactly "Group", I want it to match substrings also so "Group" would match Group1, Group2, Group 3. Is there anyway to do this? 似乎需要完全匹配,因此它仅在组名恰好是“ Group”时才适用,我也希望它也匹配子字符串,因此“ Group”将匹配Group1,Group2,Group 3。

For this you can use starts with function. 为此,您可以使用功能开头。 below is example. 以下是示例。

dt.AsEnumerable().ToList().ForEach(x =>
{
if (checkValues2.Any(t => t.StartsWith((x["Group"])))) 
{
newDT.ImportRow(x); //Copy
DT.Rows.Remove(x); //Remove
}
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM