Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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/3/xpath/2.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
Entity framework 如何使用标量值进行并集_Entity Framework - Fatal编程技术网

Entity framework 如何使用标量值进行并集

Entity framework 如何使用标量值进行并集,entity-framework,Entity Framework,在这个伪代码中,最后一行不起作用,因为标量值不可IQueryable var scalarValue = 7; var qry = (from t in db.Table where t.Filter == 5 //Arbitrary select t.ID); var unioned = qry.Union(scalarValue); 这在SQL中非常简单: select t.ID from Table union select 7; 在

在这个伪代码中,最后一行不起作用,因为标量值不可IQueryable

var scalarValue = 7;

var qry = (from t in db.Table
           where t.Filter == 5 //Arbitrary
           select t.ID);

var unioned = qry.Union(scalarValue);
这在SQL中非常简单:

select t.ID
from Table

union

select 7;
在我的场景中,我确实需要选择一个有效的Table.ID,因此解决方法是:

var scalarValue = 7;

var qry = (from t in db.Table
           where t.Filter == 5 //Arbitrary
           select t.ID);

var scalarQry = (from t in db.Table
           where t.ID == scalarValue
           select t.ID);           

var unioned = qry.Union(scalarQry);
但是,如果有办法输出,请选择7;例如,这就是我想问的问题。

写作

MyContext.MyClass.Select(t => t.Id).Union(new List<long>() { 42 }).ToList();
被调用的Union的重载为:

public static IQueryable<TSource> Union<TSource>(this IQueryable<TSource> source1, IEnumerable<TSource> source2) 
in System.Linq.Queryable


我希望这将符合您的需要

trued var unioned=qry.Unionnew List{scalarValue}@不返回iqueryable的jbl,并且该部分不会生成要在服务器上运行的SQL。我猜您没有尝试。你可以看看我的答案。@jbl我的错误,我回家时检查过了-它工作得非常好。注意:如果你想实现所有的联合,请使用Concat。
public static IQueryable<TSource> Union<TSource>(this IQueryable<TSource> source1, IEnumerable<TSource> source2)