使用两个不同视图的结果集(来自 if 语句)
我有一些代码可以处理名为“结果”的结果集(我知道原来的),但根据传入的变量,我想触发特定的查询。我在 if 语句中有以下内容,但这只会使“结果”记录集出现那些令人讨厌的红线,并且不起作用。我确信这很容易解决。
if (area == "dashboard")
{
IQueryable<ViewGetNavigationMainItem> result = (from m in
_entities.ViewGetNavigationMainItems
where m.area.Trim() == area.Trim()
where m.state == "Online"
where m.parentID == parentID
orderby m.sequence ascending
select m);
}
else
{
//Get the Content Items
IQueryable<ViewGetNavigationContentItem> result = (from m in
_entities.ViewGetNavigationContentItems
where m.navID == navID
where m.parentID == parentID
orderby m.contentOrder ascending
select m);
}
maxRecords = result.Count();
foreach (var item in result)
{
//do something
}
I have a bit of code that works with a result set called "result" (original I know) but depending on the incoming variable I'd like to fire the specific query depending. I have the below in an if statement but that just makes the "result" recordset gets those nasty red lines and it doesn't work. I'm sure this is easy to work out.
if (area == "dashboard")
{
IQueryable<ViewGetNavigationMainItem> result = (from m in
_entities.ViewGetNavigationMainItems
where m.area.Trim() == area.Trim()
where m.state == "Online"
where m.parentID == parentID
orderby m.sequence ascending
select m);
}
else
{
//Get the Content Items
IQueryable<ViewGetNavigationContentItem> result = (from m in
_entities.ViewGetNavigationContentItems
where m.navID == navID
where m.parentID == parentID
orderby m.contentOrder ascending
select m);
}
maxRecords = result.Count();
foreach (var item in result)
{
//do something
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注意:您可以使用
&&
而不是多个where
。首先,在
if
之前将result
定义为两种结果类型的基类或接口(在 C# 4.0 中)的 IQueryable 或作为Enumerable.您可以创建这样的基类。假设
ViewGetNavigationMainItem
和ViewGetNavigationContentItem
的唯一公共基类是Object
:在
foreach
中,而不是 < code>var 使用ViewGetNavigationMainItem
和ViewGetNavigationContentItem
的基类(或通用接口)。如果您没有比Object
更具体的基类,请使用object
。Note: You can use
&&
instead of multiplewhere
.First, define
result
before theif
as an IQueryable to the base class or interface of the two types of results (in C# 4.0) or as anEnumerable
. You can create such a base class. Let's say that the only common base class forViewGetNavigationMainItem
andViewGetNavigationContentItem
isObject
:In the
foreach
, instead ofvar
use the base class (or common interface) for bothViewGetNavigationMainItem
andViewGetNavigationContentItem
. If you don't have a more specific base class thanObject
useobject
.在您的代码中,当您检查计数时,“结果”不在范围内,因为它是在“if”和“else”中声明的,
您需要将结果声明移至 if 上方,并将其声明为结果是,例如 IEnumerable。
如果您只想对它们进行计数,那就是...
如果您想做更多,请考虑创建一个模型,您可以将两个结果都选择到其中,例如
in your code, "result" is not in scope when you check the count as it is declared in the "if" and "else"
you need to move the declaration of result above the if, and declare it as something that both the results are, eg IEnumerable.
if all you want to do is count them that is...
if you want to do more, consider creating a model which you can select both results into, e.g.