简体   繁体   English

比较列表元素和字符串

[英]Compare list elements with string

I have a list of db table type I want to access list elements and compare with string but I cannot access list elements How to do it? 我有一个数据库表类型的列表,我想访问列表元素并与字符串进行比较,但无法访问列表元素。该怎么做?

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

After executing the query and storing the query result in list the customattribute is only returning count of elements in list but i want elements in string 执行查询并将查询结果存储在列表中后,customattribute仅返回列表中元素的数量,但是我想要字符串中的元素

Access it like: 像这样访问它:

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

If the varchar column is all you want then you can directly select it: 如果只需要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();
}

Try customattribute.ForEach(i => Console.Write("{0}\\t", i)); 尝试customattribute.ForEach(i => Console.Write("{0}\\t", i)); and see what is showing on console? 看看控制台上显示了什么?

If you know the column or property in Tbl_UserCustomField class which holds the string you are looking for, iterate through the customattribute and fetch the string. 如果您知道Tbl_UserCustomField类中包含要查找的字符串的列或属性,请遍历customattribute并获取该字符串。 Basically, you will have to capture such result in a variable of type List<string> . 基本上,您必须在List<string>类型的变量中捕获此类结果。 This can be done in a plain foreach-way OR using a Select Linq expression that just retrieves the column you specify 可以以普通方式进行操作,也可以使用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