[英]How to use var more than once in linq?
var ID, x,y
switch(ItemTypeNo)
{
case ItemType.A :
ID = from s in cntx.Tablo1
break;
case ItemType.B :
ID = from s in cntx.Tablo2
break;
case ItemType.C :
ID = from s in cntx.Tablo3
break;
}
You can only use var
for inline initialization, which that isn't. 您只能将
var
用于内联初始化,事实并非如此。 You would need to type ID
appropriately, and since var
has a single type that would only work if Tablo1
, Tablo2
and Tablo3
are the same type (which seems unlikely). 您将需要适当地键入
ID
,并且由于var
具有仅在Tablo1
, Tablo2
和Tablo3
是相同类型(这似乎不太可能)时才起作用的单一类型。
What is it you need to do here? 您在这里需要做什么?
There is a scenario that works here; 还有的是 ,在这里工作的情形; when selecting a common type from each;
从每个中选择一个通用类型时; let's assume thay all have an
int
primary key: 让我们假设它们都具有一个
int
主键:
IQueryable<int> ids;
switch(ItemTypeNo)
{
case ItemType.A : ids= from s in cntx.Tablo1 select s.Id; break;
case ItemType.B : ids= from s in cntx.Tablo2 select s.Id; break;
case ItemType.C : ids= from s in cntx.Tablo3 select s.Id; break;
default: throw new InvalidOperationException();
}
However, in the general case... not so much. 但是,在一般情况下...并不是很多。 You could type
ID
as the non-generic IQueryable
, but to be honest that doesn't let you do very many interesting things. 您可以将
ID
键入为非通用的IQueryable
,但老实说,这不能让您做很多有趣的事情。 And dynamic
doesn't play nicely with LINQ (and even if it did, it would be a hack here). dynamic
在LINQ上不能很好地发挥作用(即使这样做,在这里也很容易被破解)。
The anonymous type "var" can not be declared without having it initialized. 如果没有初始化,则不能声明匿名类型“ var”。 Once "var" is initialized (once it is inferred from usage), it can't be altered throughout the course of its lifetime.
一旦初始化了“ var”(一旦从使用情况推断出),就无法在其整个生命周期中对其进行更改。
您可以将ID声明为IQueryable
因为这是通用IQueryable<T>
基本类型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.