-8

How can i search in LINQ as stated below?? I Want to enter a string like this "a%b%c%d%" in my textbox and want result as we get in SQL.

Select * 
from TableName 
Where ColumnName Like 'a%b%c%d%'
  • Linq-To-Sql or Linq-To-Entities. – Tim Schmelter Dec 14 '15 at 13:41
  • Possible duplicate of [How to do SQL Like % in Linq?](http://stackoverflow.com/questions/835790/how-to-do-sql-like-in-linq) – Timothée Bourguignon Dec 14 '15 at 13:49
  • @TimBourguignon: although the acepted answer doesn't help. But an answer on this question depends on the architecture, in Linq-To-Sql you could use `SqlMethods.Like`. I don't know how it works in LInq-To-Entities. I guess you need [this approach](http://stackoverflow.com/a/3095963/284240). – Tim Schmelter Dec 14 '15 at 13:54
  • I also use Like operater but it could'nt work. – Talmeez Hussain Dec 14 '15 at 14:00
  • @Roberto it's a valid SQL Query. You can check it. – Talmeez Hussain Dec 14 '15 at 14:01
  • @TalmeezHussain: you also use `LIKE` operator? How do you use it, there is no `LIKE` operator unless you don't use `VB.NET`(which works differently and has nothing to do with LINQ). Show your LINQ query. You also still haven't answered the question whether you use Linq-To-Sql or Linq-To-Entities. – Tim Schmelter Dec 14 '15 at 14:06
  • var myvar = from q in query where SqlMethods.Like(q.UserName, firstname) select q; where firstname = "a%b%c%d%" – Talmeez Hussain Dec 14 '15 at 14:10

2 Answers2

0

LINQ doesn't have like operator, so you could first check if it contains a, b, c and d, then check if a is at start, b is before c, and c before d. Like this:

from item in context.TableName
where item.ColumnName.StartsWith("a") && item.ColumnName.IndexOf("b") != -1
&& item.ColumnName.IndexOf("c") != -1 && item.ColumnName.IndexOf("d") != -1
&& (
    item.ColumnName.IndexOf("b") < item.ColumnName.IndexOf("c")
    && item.ColumnName.IndexOf("c") < item.ColumnName.IndexOf("d")
   )   
select item;
Roberto
  • 2,115
  • 24
  • 32
-1
FROM item in context.TableName
WHERE item.ColumnName.StartWith("a%") 
   OR item.ColumnName.StartWith("b%") 
   OR item.ColumnName.StartWith("c%") 
   OR item.ColumnName.StartWith("d%")   
SELECT item;
Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
  • That's not the same as OP's sql. You check if it starts with a or b or c or d. OP wants only rows where the column starts with a, then arbitrary, then b, then arbitrary, then c, then arbitrary, then d, then arbitrary. – Tim Schmelter Dec 14 '15 at 13:58