如何将从此Linq查询中检索到的值检查为.ToLower()?

Arv*_*yne 2 c# linq indexof

这是我的代码:

DataRow r = VirtualTable
  .AsEnumerable()
  .FirstOrDefault(tt => (tt.Field<string>("Column1") == value1) ||
                        (tt.Field<string>("Column1") == value2));
Run Code Online (Sandbox Code Playgroud)

此代码检索"Column1"与给定字符串匹配的数据行.然后我对bool if语句进行检查.但是,尽管我可以更改字符串的大小写,但我不知道如何使用Linq给我的值来处理它.还在学习linq,所以我还不知道我的方式.

简而言之,我在表中有字符串"Red box",但希望它被读作"红色框",因此它将匹配相同值的内部字符串.

另外,我试图检索IndexOf此查询给我的行,但即使找到匹配,我也总是检索​​-1.

这是检索它的代码:

int SelectedIndex = VirtualTable.Rows.IndexOf(r);
Run Code Online (Sandbox Code Playgroud)

Dmi*_*nko 5

尝试string.Equals忽略大小写和重载Select以获取行的索引:

   var row = VirtualTable
     .AsEnumerable()
     .Select((tt, index) => new {
        value = tt.Field<string>("Column1"),
        index = index})
     .FirstOrDefault(item => 
        string.Equals(item.value, value1, StringComparison.OrdinalIgnoreCase) ||
        string.Equals(item.value, value2, StringComparison.OrdinalIgnoreCase));

   // If we have the row found, we can get
   if (row != null) {
     var r = row.value;              // value, e.g. "bla-bla-bla"
     int selectedIndex = row.index;  // as well as its index, e.g. 123 
     ... 
   }
Run Code Online (Sandbox Code Playgroud)