[英]In List<string>, how to use the Contains() method and check if value is empty
I have a List<string>
and some saved values taken from a gridview. 我有一个
List<string>
和一些从gridview获取的保存值。 What I need is to use a few if statements in order to check if one of these values in the list are empty. 我需要的是使用一些if语句来检查列表中的其中一个值是否为空。
A simple for loop going through all the rows in the gridview and taking values from the right column: 一个简单的for循环遍历gridview中的所有行并从右列获取值:
for (int i = 0; i < GridView2.Rows.Count; i++)
{
string tasks = GridView2.Rows[i].Cells[3].Text;
datesList.Add(tasks);
}
Here is a very simple example of code I'm using to check if 2 is in the list: 这是一个非常简单的代码示例,我用来检查列表中是否有2:
if (datesList.Contains("2"))
{
Label1.Text = "It contains it";
}
else
{
Label1.Text = "No matches";
}
So does the list saves all values from the gridview no matter that some are empty? 那么列表是否会保存gridview中的所有值,无论哪些值为空?
Correct me if I'm wrong somewhere but it's really confusing now... 纠正我,如果我错了,但现在真的很混乱......
使用Any()
而不是这个。
datalist.Any(s => s.fieldname == "2")
if(datesList.Any(date => !String.IsNullOrEmpty(date)))
{
//save
}
To collect the set/filled fields: 要收集设置/填充字段:
var setDates = datesList.Select(date => !String.IsNullOrEmpty(date));
So your list of items should contain all values even if they are ""
so if you want a specific item then you can do something like this 因此,您的项目列表应包含所有值,即使它们是
""
因此如果您想要特定项目,那么您可以执行类似的操作
datalist.FirstOrDefault(i => i.FieldName == "//insert Data here");
if you want all but the items with string.Empty then you can do something like this 如果你想要除了使用string.Empty的所有项目,那么你可以做这样的事情
datalist.RemoveAll(i => i.FieldName != "");
if you want the items within a certain range then try something like this. 如果你想要在一定范围内的物品,那么尝试这样的事情。
datalist.Select(i => i.FieldName.Contains("//insert criteria here"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.