Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#LInQ中使用like子句_C#_Linq - Fatal编程技术网

如何在C#LInQ中使用like子句

如何在C#LInQ中使用like子句,c#,linq,C#,Linq,如何使用以下LINQ代码段使用like子句 var query = from r in document.Descendants("Employee") where (string)r.Element("FirstName").Value == txtSearch.Text select new {

如何使用以下LINQ代码段使用like子句

var query = from r in document.Descendants("Employee")
                        where (string)r.Element("FirstName").Value == txtSearch.Text
                        select new
                        {

                            FirstName = r.Element("FirstName").Value,
                            Age = r.Element("Age").Value
                        }; 
我尝试了以下方法,但无效:

var query = from r in document.Descendants("Employee")
                        where (string)r.Element("FirstName").Value.Contains(txtSearch.Text)
                        select new
                        {

                            FirstName = r.Element("FirstName").Value,
                            Age = r.Element("Age").Value
                        };

…提前感谢您的帮助

您正在尝试将布尔值转换为字符串:

where (string)r.Element("FirstName").Value.Contains(txtSearch.Text)
如果值确实需要转换为字符串,则代码应为:

where ((string)r.Element("FirstName").Value).Contains(txtSearch.Text)
但我认为您不需要它,因为如果VS已完成
Contains
它是一个字符串函数,不需要强制转换:

where r.Element("FirstName").Value.Contains(txtSearch.Text)

请描述如何“它不工作”我得到以下错误:无法将类型“bool”转换为“string…”。。。错误是在这一行生成的:where(string)r.Element(“FirstName”).Value.Contains(txtSearch.Text)这似乎是一条相当直接的错误消息,对吗<代码>包含返回一个
bool
,您试图将其转换为
字符串
。您应该将其作为bool,因为这正是
Where
子句所需要的。