一个字符串值与多个字符串值进行比较,类似于attributetocheck.Equals(att1,att2,att3)。

4

我有多个属性,比如att1、att2、att3,它们可以由任何分隔符分开。 我希望在下面的查询中使用“OR”条件来检查列1中的行值是否相等。 由于.Equals只能使用字符串,有人能提供其他方法吗?

string result = string.Join(",", attributes);
            List<string> query = (from DataRow dr in response.Output.Tables[0].Rows
                                         where dr["COLUMN1"].ToString().Equals(result)
                                         select dr["COLUMN2"].ToString()).ToList<string>();

1
你可以尝试使用 contains 吗? - Christian Phillips
1
https://dev59.com/pG025IYBdhLWcg3wYFCHhttp://stackoverflow.com/questions/18336480/fast-multiple-string-compare - Thomas
这两个都是相关的。后者甚至在其答案中使用了LINQ。 - Thomas
@christiandev 帮了大忙。非常感谢。 - NewInCoding
@Thomas 在提供的链接中,有点像在我的代码中硬编码了它...需要帮助...谢谢。 - NewInCoding
1个回答

3

不要将属性连接到字符串中,而是使用其Contains方法

List<string> query = (from DataRow dr in response.Output.Tables[0].Rows                                         
                      where attributes.Contains(dr["COLUMN1"].ToString())
                      select dr["COLUMN2"].ToString()).ToList<string>()

非常感谢您的详细解释。谢谢。 - NewInCoding

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接