Tech Talk A software developer on all things tech, and then some

31Jan/130

Argument data type xml is invalid for argument 1 of like function

If you haven't realized it by now, you cannot do a LIKE without any magic on a XML column. If you do, you will get the following error :

Argument data type xml is invalid for argument 1 of like function

If you need to do a LIKE on a column with XML, there are a few ways to go about it. I however for the most part just need to do this through SQL Server Management Studio, so I use the quick solution of converting it to a varchar field, and problem solved. This would look like the following :

SELECT * FROM [tableName]
WHERE CAST([XMLColumnName] AS nvarchar(max)) LIKE N'%[textToSearchFor]%'

Hope this helps and happy coding!

Tagged as: , , No Comments
11Mar/110

Do a Like Statement in LINQ in C#

There has been a few times I needed to do a like statement in LINQ, but didn't know exactly how to go about it. Just in case there are you out there looking for the same, I thought I would post what I did. For example, say you had this SQL statement :

SELECT * FROM Autos WHERE Manufacturer LIKE '%Fo%'

This would translate to LINQ code in C# like the following

dbContext.Autos.Where(a=> a.Manufacturer.Contains("Fo")).AsEnumerable();
Tagged as: , , , No Comments
   
%d bloggers like this: