开发者

Why can't I use var in this instance?

I am using th开发者_StackOverflow中文版e 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.


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.


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};


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.


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 } );


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜