簡體   English   中英

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

[英]How to use like Clause in C# LInQ

我將如何使用以下 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)

如果 Value 真的需要轉換為 String 代碼應該是這樣的:

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

但我認為你不需要它,因為如果 VS 已經完成Contains它是一個字符串函數並且不需要強制轉換:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM