Click here to Skip to main content
14,363,337 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want to convert the following SQL query to linq or iqueryable:

string title = "motor pumps";

"SELECT c.ID as ID,
                                                                                         c.Description as Description, 
                                                                                         m.MfgName as MfgName
                                                                                           FROM swCompanyEqpt c left join swMfg m on c.swMfgKey = m.ID
                                                                                           WHERE ((c.FleetEqptName LIKE '%" + title.Replace(" ", "%' OR c.FleetEqptName LIKE '%") + "%')" +
                                                                            "OR (m.MfgName LIKE '%" + title.Replace(" ", "%' OR m.MfgName LIKE '%") + "%')"  +
                                                                            "order by c.FleetEqptName"


Consider the following for writing the above query:

var lstSwCompanyEqptDto = from swCompanyEqpt in dbContext.Set<swcompanyeqpt>()

What I have tried:

I have tried to convert like operator in SQL to LINQ for multiple words search
Posted
Updated 20-Oct-16 10:06am
Comments
F-ES Sitecore 20-Oct-16 4:32am
   
google "linq like" and you'll find lots of suggestions, please do basic research before asking a question.
info2santram 20-Oct-16 5:53am
   
will you plz answer for my question. did you get my question?
Karthik_Mahalingam 20-Oct-16 4:52am
   
"contains" is the equivalent operator for "Like" in c#
info2santram 20-Oct-16 5:56am
   
hi i know that see my below comment
info2santram 20-Oct-16 5:52am
   
i want to search any combination from a given string. Eg: "control pump"

1 control
2 pump
3 pump control
4 control pump
Karthik_Mahalingam 20-Oct-16 11:41am
   
split the string and search the collection
Rate this:
Please Sign up or sign in to vote.

Solution 1

Quote:
i want to search any combination from a given string. Eg: "control pump"

1 control
2 pump
3 pump control
4 control pump



refer this example

using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
    static void Main()
    {
        List<Entity> lst = new List<Entity>();
        lst.Add(new Entity() { MyProperty = "control item" });
        lst.Add(new Entity() { MyProperty = "pump item" });
        lst.Add(new Entity() { MyProperty = "control pump" });
        lst.Add(new Entity() { MyProperty = "item " });
        string key = "control pump";
        Func<Entity, bool> like = k =>
        {
            var temp = key.Split(' ').ToList();
            temp.Add(key);
            foreach (var item in temp)
                if (k.MyProperty.Contains(item))
                    return true;
            return false;
        };
        var result = lst.Where(like).ToList();
    }
}
public class Entity
{
    public string MyProperty { get; set; }

}
   
Comments
Maciej Los 20-Oct-16 15:06pm
   
5ed!
Karthik_Mahalingam 20-Oct-16 23:44pm
   
Thank you Maciej
Wendelius 21-Oct-16 13:49pm
   
Nice example, a 5
Karthik_Mahalingam 22-Oct-16 20:03pm
   
Thank you Mika
Rate this:
Please Sign up or sign in to vote.

Solution 2

Based on Karthik Bangalore[^] code sample, i'd use linq query like this:

string key = "control pump";
string[] keys = key.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
//filter data
var result = lst.Where(e=>keys.Any(k=>e.MyProperty.Contains(k)))
                .ToList();


For further details, please see:
Enumerable.Contains(TSource) Method (IEnumerable(TSource), TSource) (System.Linq)[^]
Enumerable.Any(TSource) Method (IEnumerable(TSource)) (System.Linq)[^]
Enumerable.Where(TSource) Method (IEnumerable(TSource), Func(TSource, Boolean)) (System.Linq)[^]
   
Comments
Karthik_Mahalingam 20-Oct-16 23:46pm
   
simple & nice
5!
Maciej Los 21-Oct-16 10:46am
   
Thank you, Karthik.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100