gpt4 book ai didi

c# - 如何使用 LINQ 根据需要拆分的字符串列表过滤数据表?

转载 作者:太空宇宙 更新时间:2023-11-03 11:00:05 25 4
gpt4 key购买 nike

我有一个数据表,我想使用 LINQ 过滤字符串列表,每个字符串使用竖线 ('|') 分隔,并包含两个值。字符串列表(列表操作)如下所示。这只是此列表中的两个字符串,但它可以有更多。

    8/1/2013 9:57:52 PM|Login for bill.lock@cap.com
8/1/2013 9:57:37 PM|Login for bill.lock@cap.com

数据表每行有五 (5) 个字段,我使用上面列表中的每个字符串来比较数据表中的两个字段(文本和时间)以忽略或删除这些行。数据表的结构是这样的

    DataTable stdTable = new DataTable("Actions");
DataColumn col1 = new DataColumn("Area");
DataColumn col2 = new DataColumn("Action");
DataColumn col3 = new DataColumn("Time");
DataColumn col4 = new DataColumn("Text");

目前我正在手动执行所有这些操作,但我知道只需几行代码就可以在 LINQ 中完成。我不确定如何遍历列表并使用拆分。我看到了这个例子,但 split 超出了我的范围。

    // Get all checked id's.
var ids = chkGodownlst.Items.OfType<ListItem>()
.Where(cBox => cBox.Selected)
.Select(cBox => cBox.Value)
.ToList();

// Now get all the rows that has a CountryID in the selected id's list.
var a = dt.AsEnumerable().Where(r =>
ids.Any(id => id == r.Field<int>("CountryID"))
);

// Create a new table.
DataTable newTable = a.CopyToDataTable();

如有任何帮助,我们将不胜感激。

谢谢

最佳答案

List<string> list = {
"8/1/2013 9:57:52 PM|Login for bill.lock@cap.com",
"8/1/2013 9:57:37 PM|Login for bill.lock@cap.com"
};
var a = dt.AsEnumerable().Where(x=>
!list.Select(y=> new {
Time = DateTime.Parse(y.Split('|')[0]),
Text = y.Split('|')[1]
})
.Any(z=> z.Time == x.Time && z.Text == x.Text));

var a = dt.AsEnumerable().Where(x=>
!list.Any(y=> y == string.Format("{0}|{1}",x["Time"],x["Text"])));

DataTable newTable = a.CopyToDataTable();

关于c# - 如何使用 LINQ 根据需要拆分的字符串列表过滤数据表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18007726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com