繁体   English   中英

比较列表元素和字符串

[英]Compare list elements with string

我有一个数据库表类型的列表,我想访问列表元素并与字符串进行比较,但无法访问列表元素。该怎么做?

List<Tbl_UserCustomField> customattribute = (from custom in tniDataContext.Tbl_UserCustomFields 
                                             where workOrderIndex.LoginId == custom.LoginId 
                                             select custom).ToList();

执行查询并将查询结果存储在列表中后,customattribute仅返回列表中元素的数量,但是我想要字符串中的元素

像这样访问它:

foreach(var item in customattribute)
{
    if(item.SomeField == "someString")
       DoSomething();
    else
       DoSomethingElse();
}

如果只需要varchar列,则可以直接选择它:

var customattribute = (from custom in tniDataContext.Tbl_UserCustomFields 
                       where workOrderIndex.LoginId == custom.LoginId 
                       select custom.SomeColumn).ToList();

foreach(var item in customattribute)
{
    if(item == "someString")
       DoSomething();
    else
       DoSomethingElse();
}

尝试customattribute.ForEach(i => Console.Write("{0}\\t", i)); 看看控制台上显示了什么?

如果您知道Tbl_UserCustomField类中包含要查找的字符串的列或属性,请遍历customattribute并获取该字符串。 基本上,您必须在List<string>类型的变量中捕获此类结果。 可以以普通方式进行操作,也可以使用Select Linq表达式(仅检索您指定的列)来完成

        // Approach 1:
        List<string> customAttributeValues = customattribute.Select(c => c.YourStringFieldName).ToList();

        // Approach 2:
        List<string> customAttributeValues = new List<string>();
        foreach (var custom in customattribute)
        {
            customAttributeValues.Add(custom.YourStringFieldName);
        }

暂无
暂无

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

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