Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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查询sytax中的预定义表达式_C#_Linq_Entity Framework_Expression_Linq Query Syntax - Fatal编程技术网

C# LINQ查询sytax中的预定义表达式

C# LINQ查询sytax中的预定义表达式,c#,linq,entity-framework,expression,linq-query-syntax,C#,Linq,Entity Framework,Expression,Linq Query Syntax,是否可以使用以前在LINQ表达式中定义的?我创造了一个人为的例子来说明我的问题 var tabletTypes = new List<int> { 2, 3 }; //I want to use this expression many times Expression<Func<Computer, bool>> isTablet = comp => tabletTypes.Contains(comp.Type); var computers = db

是否可以使用以前在LINQ表达式中定义的?我创造了一个人为的例子来说明我的问题

var tabletTypes = new List<int> { 2, 3 };
//I want to use this expression many times
Expression<Func<Computer, bool>> isTablet = comp => tabletTypes.Contains(comp.Type);

var computers = db.Computers;
var producers = db.Producers;

//I know how to use the expression in fluent syntax
IQueryable<Producer> tabletProducers = computers
                               .Where(isTablet)
                               .Join(producers, 
                                     comp => comp.ProducerId, 
                                     producer => producer.Id, 
                                     (comp, producer) => producer);

//How can I use the expression in query syntax
IQueryable<Producer> tabletProducers2 = from c in computers
                                        join p in producers
                                            on c.ProducerId equals p.Id
                                        where /*How can I use isTablet here?*/
                                        select p;


public class Producer
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Computer
{
    public int Id { get;set; }
    public int Type { get; set; }
    public int ProducerId { get; set; }
}
var tabletTypes=新列表{2,3};
//我想多次使用这个表达式
表达式isTablet=comp=>tabletTypes.Contains(comp.Type);
var computers=db.computers;
var生产者=分贝生产者;
//我知道如何用流利的语法来表达
IQueryable tabletProducers=计算机
.何处(iTablet)
.加入(制作人),
comp=>comp.ProducerId,
producer=>producer.Id,
(comp,producer)=>producer);
//如何在查询语法中使用表达式
IQueryable tabletProducers2=来自计算机中的c
加入生产商
关于c.ProducerId等于p.Id
在哪里/*如何在此处使用iTablet*/
选择p;
公共级制作人
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
公共类计算机
{
公共int Id{get;set;}
公共int类型{get;set;}
public int ProducerId{get;set;}
}

我使用的是C#5和EF 5。

您可以使用
where-isTablet.Compile()(C)
。您可能希望缓存已编译的版本。

使用此选项会产生实体框架错误“LINQ to Entities中不支持LINQ表达式节点类型'Invoke'。为了进一步解释我的注释…此答案适用于
IEnumerables
,但不是
IQueryables
。如果使用表达式树,则可以这样做。请看一看,尤其是
Expand()
功能。@SimonBelanger我从未使用过LinqKit。看起来很有趣。是的,那页也解释了为什么我的答案不起作用