我正在使用下面的代码,由于某种原因,我不能使用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;
感谢任何能告诉我正确方法的人。
答案 0 :(得分:7)
如果它是一个局部变量且查询本身有效,那么你就可以。
但是,查询表达式无效。只看最后一行,这个:
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
视为主要排序顺序,其他则作为次要排序。
答案 1 :(得分:1)
修正: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};
答案 2 :(得分:0)
var公开了枚举器,它支持对指定类型的集合进行简单迭代。在您的select brand_family.brand_family_name中,优先级,roll_image; 你要退回多件物品。
答案 3 :(得分:0)
不确定是否会修复它但请尝试:
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 } );
答案 4 :(得分:0)
你也应该修理你的订单,否则你只是重新排序
orderby brand_family.brand_family_name
orderby brand.priority
orderby brand.genre_id
应该成为
orderby brand_family.brand_family_name, brand.priority, brand.genre_id