C# 实例化空的 IQueryable 以与 Linq to sql 一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14993048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Instantiate empty IQueryable for use with Linq to sql
提问by user2073077
I need to be able to either have an optional parameter in a Linq query, or be able to assign the query to a var in something like an IF if that optional parameter needs to be removed from the query.
我需要能够在 Linq 查询中有一个可选参数,或者如果需要从查询中删除该可选参数,则能够将查询分配给一个类似于 IF 的变量。
If I set the query var inside the IF statement then it tells me that the var doesn't exist in context when I try to loop through it.
如果我在 IF 语句中设置查询变量,那么当我尝试遍历它时,它会告诉我变量在上下文中不存在。
if (whichGroup == "All")
{
var listOppLineData = (from o in db.opportunity_vws
where o.fiscal_yr_and_qtr == qtr
select o
);
}
else
{
var listOppLineData = (from o in db.opportunity_vws
where o.fiscal_yr_and_qtr == qtr && o.Group == whichGroup
select o
);
}
foreach (var data in listOppLineData) //listOppLineData doesn't exist here
{
I need to set the var before the IF statement I think, but I don't know what to set it to.
我需要在我认为的 IF 语句之前设置 var,但我不知道将它设置为什么。
var listOppLineData = ""; // gives me
Cannot implicitly convert type 'System.Linq.IQueryable<Forecast.Models.opportunity_vw>' to 'string'
IQueryable listOppLineData = new IQueryable(); //gives me
Cannot create an instance of the abstract class or interface 'System.Linq.IQueryable'
采纳答案by Simon C
Try this:
尝试这个:
IQueryable<opportunity_vw> listOppLineData;
This is defining the variable, but you are waiting to initialise it.
这是定义变量,但您正在等待初始化它。
Also, looking at your query, you could do it all in one, like so:
此外,查看您的查询,您可以一次性完成所有操作,如下所示:
var listOppLineData = (from o in db.opportunity_vws
where o.fiscal_yr_and_qtr == qtr && (o.Group == whichGroup || whichGroup == "All")
select o
);
回答by Trey Gramann
Try adding .ToList();
尝试添加 .ToList();
var listOppLineData = (from o in db.opportunity_vws
where o.fiscal_yr_and_qtr == qtr
select o
).ToList();
Update
更新
And fix the query.
并修复查询。
You will not have anything to iterate over without the .ToList();
没有 .ToList(),你将没有任何东西可以迭代;
Head Out of Butt Update
头出屁股更新
var listOppLineData = (from o in db.opportunity_vws
where (whichGroup == "All" || o.Group == whichGroup)
&& (o.fiscal_yr_and_qtr == qtr)
select o);
Put your 'short' before the OR, so then the second part is never evaled if not needed. I would still use the .ToList() though.
将您的“短”放在 OR 之前,因此如果不需要,则永远不会评估第二部分。我仍然会使用 .ToList() 。
回答by svick
Your problem is that when you declare a variable in C#, you declare it only in the current scope. So, if you declare the variable listOppLineData
in the if
block, you can use it only inside that block, but not anywhere else. (You can define another variable with the same name in another block, but it would be a completely different variable, so it wouldn't have the value you assigned to the first variable.)
你的问题是当你在 C# 中声明一个变量时,你只在当前范围内声明它。因此,如果listOppLineData
在if
块中声明变量,则只能在该块内使用它,而不能在其他任何地方使用。(您可以在另一个块中定义另一个具有相同名称的变量,但它将是一个完全不同的变量,因此它不会具有您分配给第一个变量的值。)
As you already figured out, you need to declare the variable outside the if
blocks.
正如您已经知道的,您需要在if
块外声明变量。
Your first attempt didn't work, because the compiler saw that you didn't explicitly specify the type of the variable (that's what var
does) and you assigned a string
to the variable. So it inferred that the type of the variable is string
. And you can't assign something that is not a string
to such variable.
您的第一次尝试没有成功,因为编译器发现您没有显式指定变量的类型(正是var
这样做的)并且您将 a 分配string
给了变量。所以它推断出变量的类型是string
。并且您不能将不是 a 的东西分配string
给此类变量。
Your second attempt didn't work, because you can't create an instance of an interface. (If you want to create an instance, it has to be some specific type, usually a class.) And even if you fixed that, the non-generic IQueryable
doesn't know the type of items in it, so the foreach
wouldn't work well.
您的第二次尝试没有成功,因为您无法创建接口的实例。(如果你想创建一个实例,它必须是某种特定的类型,通常是一个类。)即使你修复了它,非泛型IQueryable
也不知道其中的项目类型,所以它foreach
不起作用好。
But you don't need to assign any value to the variable, since it's assigned in both branches of the if
. And the correct type here is the generic IQueryable<T>
, with T
being opportunity_vw
. That means the correct code is:
但是您不需要为该变量分配任何值,因为它在if
. 此处正确的类型是泛型IQueryable<T>
,而T
be opportunity_vw
。这意味着正确的代码是:
IQueryable<opportunity_vw> listOppLineData;
if (whichGroup == "All")
{
listOppLineData = …;
}
else
{
listOppLineData = …;
}
foreach (var data in listOppLineData)
{
…
}
If you really wanted to assign some value to the variable (although there is no reason to do that here), you could use null
(which represents no value):
如果您真的想为变量分配一些值(尽管这里没有理由这样做),您可以使用null
(表示没有值):
IQueryable<opportunity_vw> listOppLineData = null;
回答by Krishna
Try this. You can create a generic type with Tor a specific type by replacing Twith your type name.
尝试这个。您可以使用T或特定类型创建泛型类型,方法是将T替换为您的类型名称。
IQueryable listOppLineData = Enumerable.Empty<T>().AsQueryable()
回答by agileDev
It works for me:
这个对我有用:
IQueryable<string> _labTests = Enumerable.Empty<string>().AsQueryable();