Skip to main content
 首页 » 编程设计

c#之需要做搜索

2024年02月13日16落叶无声

我有以下查询:

DataTable dtble = dt.AsEnumerable() 
                    .Any(row => row.Field<String>("Course") == "Math") 
                    .CopyToDataTable(); 

我希望在 Course 包含 "Math" 的地方过滤记录。我尝试使用 .Any() 但它没有用。

澄清一下,如果 Course 有 12XMath123,我仍然应该返回一条记录,因为它包含“Math”。使用 Where 原因只会返回包含“Math”的记录,而不是包含“Math”的记录

请您参考如下方法:

尝试使用 Where() 而不是 Any()。这对我有用:

DataTable dtble = dt.AsEnumerable() 
                    .Where(row => row.Field<String>("Course") == "Math") 
                    .CopyToDataTable(); 

根据评论,您可以尝试 StartsWith() 或 EndsWith() 或 Contains()。例如:

DataTable dtble = dt.AsEnumerable() 
                    .Where(row => row.Field<String>("Course").Contains("Math")) 
                    .CopyToDataTable();