为什么我不能在这种情况下使用 var ?

发布于 2024-10-31 20:49:35 字数 549 浏览 1 评论 0原文

我正在使用下面的代码,由于某种原因我无法使用 var 来声明我的变量。我是 ASP/C# 的新手,所以请对我宽容一些。我环顾四周,找不到正确的方法:

var brandslist = from brand in brandDB.brands
                         join brand_family in brandDB.brand_family on brand.brand_family_id equals brand_family.bf_id
                         orderby brand_family.brand_family_name
                         orderby brand.priority
                         orderby brand.genre_id
                         select brand_family.brand_family_name, priority, roll_image;

感谢任何可以告诉我完成这项工作的正确方法的人。

I am using the code below and for some reason I can't use var to declare my variable. I am brand new to asp/c# so please go easy on me. I've had a look around and I can't find the correct way of doing this:

var brandslist = from brand in brandDB.brands
                         join brand_family in brandDB.brand_family on brand.brand_family_id equals brand_family.bf_id
                         orderby brand_family.brand_family_name
                         orderby brand.priority
                         orderby brand.genre_id
                         select brand_family.brand_family_name, priority, roll_image;

Thanks to anyone who can tell me the correct method of making this work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

榆西 2024-11-07 20:49:35

如果它是局部变量并且查询本身有效,那么您就可以。

但是,查询表达式无效。看看最后一行,这:

select brand_family.brand_family_name, priority, roll_image;

可能应该是:

select new { brand_family.brand_family_name,
             brand.priority,
             brand.roll_image };

值得仔细查看编译器错误 - 它通常对错误给出了相当好的解释。

接下来,你的顺序几乎肯定是错误的——我非常怀疑这是否达到了你想要的效果。要表达多个排序,您通常只需列出它们,并用逗号分隔,如下所示:

var brandslist = from brand in brandDB.brands
                 join brand_family in brandDB.brand_family
                   on brand.brand_family_id equals brand_family.bf_id
                 orderby brand_family.brand_family_name,
                         brand.priority,
                         brand.order_id
                 select new { brand_family.brand_family_name,
                              brand.priority,
                              brand.roll_image };

这将转换为 OrderBy 调用,后跟后续的 ThenBy 调用 - 处理 brand_family_name 作为主要排序顺序,其他作为次要排序。

If it's a local variable and the query itself were valid, you would be able to.

However, the query expression is invalid. Just looking at the last line, this:

select brand_family.brand_family_name, priority, roll_image;

should probably be:

select new { brand_family.brand_family_name,
             brand.priority,
             brand.roll_image };

It's worth looking closely at the compiler error - it generally gives a reasonably good explanation of what's wrong.

Next up, your ordering is almost certainly wrong - I very much doubt that this does what you want it to. To express multiple orderings, you typically just list them, separated by commas, like this:

var brandslist = from brand in brandDB.brands
                 join brand_family in brandDB.brand_family
                   on brand.brand_family_id equals brand_family.bf_id
                 orderby brand_family.brand_family_name,
                         brand.priority,
                         brand.order_id
                 select new { brand_family.brand_family_name,
                              brand.priority,
                              brand.roll_image };

This will translate to an OrderBy call followed by subsequent ThenBy calls - treating brand_family_name as the primary sort ordering, and the others as secondary orderings.

墨小沫ゞ 2024-11-07 20:49:35

固定:D

var brandslist = from brand in brandDB.brands
                 join brand_family in brandDB.brand_family on brand.brand_family_id equals brand_family.bf_id
                 orderby brand_family.brand_family_name,
                         brand.priority,
                         brand.genre_id
                 select new {brand_family.brand_family_name, brand.priority, brand.roll_image};

Fixed :D

var brandslist = from brand in brandDB.brands
                 join brand_family in brandDB.brand_family on brand.brand_family_id equals brand_family.bf_id
                 orderby brand_family.brand_family_name,
                         brand.priority,
                         brand.genre_id
                 select new {brand_family.brand_family_name, brand.priority, brand.roll_image};
╰つ倒转 2024-11-07 20:49:35

var 公开枚举器,它支持对指定类型的集合进行简单迭代。在你选择brand_family.brand_family_name,priority,roll_image;
您要退回多件商品。

var exposes the enumerator, which supports a simple iteration over a collection of a specified type. In your select brand_family.brand_family_name, priority, roll_image;
you are returning multiple items.

π浅易 2024-11-07 20:49:35

不确定它是否会修复它,但试试这个:

var brandslist = (from brand in brandDB.brands
                         join brand_family in brandDB.brand_family on brand.brand_family_id equals brand_family.bf_id
                         orderby brand_family.brand_family_name
                         orderby brand.priority
                         orderby brand.genre_id
                         select new { brand_family.brand_family_name, priority, roll_image } );

Not sure if it will fix it but try this:

var brandslist = (from brand in brandDB.brands
                         join brand_family in brandDB.brand_family on brand.brand_family_id equals brand_family.bf_id
                         orderby brand_family.brand_family_name
                         orderby brand.priority
                         orderby brand.genre_id
                         select new { brand_family.brand_family_name, priority, roll_image } );
装迷糊 2024-11-07 20:49:35

您还应该修复您的 orderby ,否则您只是重新排序

orderby brand_family.brand_family_name
orderby brand.priority
orderby brand.genre_id

应该变成

orderbybrand_family.brand_family_name,brand.priority,brand.genre_id

You should also fix your orderby or else you are just reordering

orderby brand_family.brand_family_name
orderby brand.priority
orderby brand.genre_id

Should become

orderby brand_family.brand_family_name, brand.priority, brand.genre_id

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