像C#LINQ中的运算符一样 [英] Like operator in LINQ in C#

查看:111
本文介绍了像C#LINQ中的运算符一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将以下SQL查询转换为linq或iqueryable:



string title =motor pumps;



  选择c.ID作为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( %'或c.FleetEqptName LIKE'%)+ %') +
OR(m。 MfgName LIKE'% + title.Replace( %'或m.MfgName LIKE'%)+ %') +
order by c.FleetEqptName





考虑以下内容来编写上述查询:



var lstSwCompanyEqptDto =来自dbContext.Set中的swCompanyEqpt< swcompanyeqpt>()



我尝试过:



我试图将SQL中的运算符转换为LINQ,用于多个单词搜索

解方案

引用:
我想从一个给定的搜索字符串的任意组合。例如:控制泵



1控制

2泵

3泵控制

4控制泵







参考这个例子



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
class 计划
{
静态 void Main()
{
List< Entity> lst = new 列表< Entity>();
lst.Add( new Entity(){MyProperty = control item});
lst.Add( new Entity(){MyProperty = pump item});
lst.Add( new Entity(){MyProperty = 控制泵});
lst.Add( new Entity(){MyProperty = item});
string key = control pump< /跨度>;
Func< Entity,bool>喜欢= 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 实体
{
public string MyProperty { get ; set ; }

}


基于 Karthik Bangalore [ ^ ]代码示例,我会像这样使用linq查询:



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

< br $> b $ b

详情请见:

Enumerable.Contains(TSource)方法(IEnumerable(TSource),TSource)(System.Linq) [ ^ ]

Enumerable.Any(TSource)方法(IEnumerable(TSource))(System.Linq) [ ^ ]

Enumerable.Where(TSource)方法(IEnumerable(TSource),Fu nc(TSource,Boolean))(System.Linq) [ ^ ]


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

解决方案

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; }

}


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)[^]


这篇关于像C#LINQ中的运算符一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆