Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 针对整个数据库集的LINQ(.Contains())查询_C#_Sql_Entity Framework - Fatal编程技术网

C# 针对整个数据库集的LINQ(.Contains())查询

C# 针对整个数据库集的LINQ(.Contains())查询,c#,sql,entity-framework,C#,Sql,Entity Framework,但是,我希望能够做一些类似的事情,除了搜索经销商中的所有字段,而不仅仅是名称。所有字段都是字符串 编辑 好吧,我开始觉得这不是我想要达到的最好的方式。我正在研究一种叫做“全文搜索”的东西。是否有人可以向我解释这在实体中是如何工作的,或者给我一个指向好资源的链接您可以手动为所有字段编写条件: objs = from dealer in db.Dealers where dealer.Name.Contains(q) 没有简单的方法可以告诉Entity Framework检查实体的所

但是,我希望能够做一些类似的事情,除了搜索经销商中的所有字段,而不仅仅是名称。所有字段都是字符串

编辑
好吧,我开始觉得这不是我想要达到的最好的方式。我正在研究一种叫做“全文搜索”的东西。是否有人可以向我解释这在实体中是如何工作的,或者给我一个指向好资源的链接

您可以手动为所有字段编写条件:

objs = from dealer in db.Dealers
       where dealer.Name.Contains(q)

没有简单的方法可以告诉Entity Framework检查实体的所有属性是否符合某些条件。

您可以手动为所有字段写入条件:

objs = from dealer in db.Dealers
       where dealer.Name.Contains(q)

没有简单的方法可以告诉Entity Framework检查实体的所有属性是否符合某些条件。

对不起,这里没有捷径:

objs = from dealer in db.Dealers
       where dealer.Name.Contains(q) ||
             dealer.Foo.Contains(q) ||  
             // etc
             dealer.Bar.Contains(q)
       select dealer;

您必须指定要在其中搜索值的字段。

对不起,这里没有捷径:

objs = from dealer in db.Dealers
       where dealer.Name.Contains(q) ||
             dealer.Foo.Contains(q) ||  
             // etc
             dealer.Bar.Contains(q)
       select dealer;

您必须指定要在其中搜索值的字段。

您可以编写linq扩展方法:

查看我的博客帖子。

(类也在github中:)

然后,您可以编写如下内容:

//Create SwapVisitor to merge the parameters from each property expression into one  
public class SwapVisitor : ExpressionVisitor  
{  
    private readonly Expression from, to;  
    public SwapVisitor(Expression from, Expression to)  
    {  
        this.from = from;  
        this.to = to;  
    }  
    public override Expression Visit(Expression node)  
    {  
        return node == from ? to : base.Visit(node);  
    }  
    public static Expression Swap(Expression body, Expression from, Expression to)  
    {  
        return new SwapVisitor(from, to).Visit(body);  
    }  
}  

您可以编写linq扩展方法:

查看我的博客帖子。

(类也在github中:)

然后,您可以编写如下内容:

//Create SwapVisitor to merge the parameters from each property expression into one  
public class SwapVisitor : ExpressionVisitor  
{  
    private readonly Expression from, to;  
    public SwapVisitor(Expression from, Expression to)  
    {  
        this.from = from;  
        this.to = to;  
    }  
    public override Expression Visit(Expression node)  
    {  
        return node == from ? to : base.Visit(node);  
    }  
    public static Expression Swap(Expression body, Expression from, Expression to)  
    {  
        return new SwapVisitor(from, to).Visit(body);  
    }  
}  
db.Dealers.Search(q, x => x.Field1,    
                     x => x.Field2, 
                     ...  
                     x => x.Field20)