简体   繁体   English

在列表中 <string> ,如何使用Contains()方法并检查值是否为空

[英]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.

相关问题 如何检查字符串是否包含列表值以及是否单独包含列表值和其他值 - How to check if string contains list value and separately if contains but with other value 如何使用 Linq 检查字符串列表是否包含列表中的任何字符串 - How to use Linq to check if a list of strings contains any string in a list 检查字符串是否包含数组中的字符串,然后在LastIndexOf方法中使用它 - Check if string contains string from an array then use that in LastIndexOf method 如何检查ViewBag中的列表是否包含字符串 - How to check if a List in a ViewBag contains string 如何检查字典是否在通用列表中包含值 - How to check if dictionary contains a value in a generic list 如何检查字符串是否包含 EpiFind 中的值? - How to check if string contains value in EpiFind? 检查列表中的值<class>包含在任何列表中<string>使用 Lambda</string></class> - Check value in a List<Class> contains in any List<String> usingLambda 检查字符串列表是否包含任何枚举字符串值 - Check if string list contains any enum string value 如何使用 Linq where 条件检查字符串列表是否包含任何字符串 - How to use Linq where condition to check if a list of strings contains any string 检查数据是否包含来自 LINQ 字符串列表的值 - Check if data contains a value from List of string in LINQ
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM