使用两个不同视图的结果集(来自 if 语句)

发布于 2024-09-05 10:14:49 字数 925 浏览 6 评论 0原文

我有一些代码可以处理名为“结果”的结果集(我知道原来的),但根据传入的变量,我想触发特定的查询。我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

○愚か者の日 2024-09-12 10:14:49

注意:您可以使用 && 而不是多个 where

首先,在 if 之前将 result 定义为两种结果类型的基类或接口(在 C# 4.0 中)的 IQueryable 或作为 Enumerable.您可以创建这样的基类。假设 ViewGetNavigationMainItemViewGetNavigationContentItem 的唯一公共基类是 Object

 IQueryable<object> result;

 if (area == "dashboard")
                        {
                            result = (from m in _entities.ViewGetNavigationMainItems
                                      where m.area.Trim() == area.Trim()
                                      && m.state == "Online"
                                      && m.parentID == parentID
                                      orderby m.sequence ascending
                                      select m);  
                        }
                        else
                        {
                            //Get the Content Items
                            result = (from m in _entities.ViewGetNavigationContentItems
                                      where m.navID == navID
                                      && m.parentID == parentID
                                      orderby m.contentOrder ascending
                                      select m);
                        }

foreach 中,而不是 < code>var 使用 ViewGetNavigationMainItemViewGetNavigationContentItem 的基类(或通用接口)。如果您没有比 Object 更具体的基类,请使用 object

foreach (object item in result)
                        { etc etc etc

Note: You can use && instead of multiple where.

First, define result before the if as an IQueryable to the base class or interface of the two types of results (in C# 4.0) or as an Enumerable. You can create such a base class. Let's say that the only common base class for ViewGetNavigationMainItem and ViewGetNavigationContentItem is Object:

 IQueryable<object> result;

 if (area == "dashboard")
                        {
                            result = (from m in _entities.ViewGetNavigationMainItems
                                      where m.area.Trim() == area.Trim()
                                      && m.state == "Online"
                                      && m.parentID == parentID
                                      orderby m.sequence ascending
                                      select m);  
                        }
                        else
                        {
                            //Get the Content Items
                            result = (from m in _entities.ViewGetNavigationContentItems
                                      where m.navID == navID
                                      && m.parentID == parentID
                                      orderby m.contentOrder ascending
                                      select m);
                        }

In the foreach, instead of var use the base class (or common interface) for both ViewGetNavigationMainItem and ViewGetNavigationContentItem. If you don't have a more specific base class than Object use object.

foreach (object item in result)
                        { etc etc etc
如歌彻婉言 2024-09-12 10:14:49

在您的代码中,当您检查计数时,“结果”不在范围内,因为它是在“if”和“else”中声明的,

您需要将结果声明移至 if 上方,并将其声明为结果是,例如 IEnumerable。

如果您只想对它们进行计数,那就是...

如果您想做更多,请考虑创建一个模型,您可以将两个结果都选择到其中,例如

class ResultModel{
 int Id{get;set;}
 string Display{get;set;}
}

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.

class ResultModel{
 int Id{get;set;}
 string Display{get;set;}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文