Solved

MVC dropdownlist is showing duplicate options

Posted on 2013-10-29
6
522 Views
Last Modified: 2013-10-30
In my Controller I've created a dropdownlist:

     ViewBag.Year = new SelectList(db.Inventorys, "InventoryId", "Year");

In my View it is written like this:

     @Html.DropDownList("Year", null, "All Years", new { onchange = "this.form.submit()" })

I have many inventory with the same year, so the list has, for example, the year 2010 listed 5 times.

How do I select only unique years using the above example? If you could provide an example - to avoid the possibility that I mightn't understand your answer completely.

Thank you!
0
Comment
Question by:DonHoddinott2
  • 3
  • 2
6 Comments
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
ID: 39610974
Hi,

Try the following in your controller:

ViewBag.Year = new SelectList
    (
        db
        .Inventorys
        .GroupBy(x=>x.Year)
        .Select(x=>new {InventoryId = x.Max(z=>z.InventoryId), Year = x.Key}), 
        "InventoryId", 
        "Year"
    );

Open in new window


Giannis
0
 
LVL 16

Expert Comment

by:Stephan
ID: 39611164
If you have 5 different inventories for 1 year, how do you want people to select a particular inventory or do you want them to select all inventories.

If you prefer to select the latest inventoryId, you can pick jyparask's solution.
0
 

Author Closing Comment

by:DonHoddinott2
ID: 39611511
Thanks Giannis, that worked perfectly! I'm really grateful.

If you have a chance, do you mind explaining That lmbda expression? I'm new, and want to understand.

db
.Inventorys // Select inventorys
.GroupBy(x => x.Year) // Group by year
.Select(x=>new {InventoryId = x.Max(z=>z.InventoryId), Year = x.Key} // ???
"InventoryId", // Which column to use
"Year" // Which column to display as text
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 39611629
Any time...:)

SelectList takes three arguments. The first is an expression that fetches data (i will explain the specific one later).

The second argument is a string and shows the "value" member of the dropdown.

The third is again a string and shows the "display" member of the dropdown (what the user sees in the list).

Now for the Lambda:

 db   //This is your db context
.Inventorys //This is the table in your db
.GroupBy(x=>x.Year) //I group by the Year column as i need to fetch only once each year.
.Select(x=>new {InventoryId = x.Max(z=>z.InventoryId), Year = x.Key}) //Here i take the Key which is what we have in the GroupBy section. In this case we have only one field so this means that it takes the Year field. If we wanted to GroupBy by two (or more) fields, then we would create an Anonymous object like (.GroupBy(x=>new{x.Year,x.Month})). In that case we would access these like x.Key.Year and x.Key.Month . Lastly i get the as an InventoryId the maximum InventoryId  for each group key (Year in this case)

The above lambda would be equivalent to the following SQL (though it is not interpreted exactly like this by EF or LINQ2SQL):

SELECT MAX(InventoryId),
             Year
FROM   Inventory
GROUP BY Year

Open in new window


By the way, you will find very handy LINQPad (DISCLAIMER, i am not trying to advertise, this is a free to use tool and it has helped me a lot, thus i recomend it), as you may write your LINQ and see what is the equivalent SQL executed against the DB.

Let me know if this was clear or not...

Giannis
0
 

Author Comment

by:DonHoddinott2
ID: 39611674
Hi Giannis,

I think you were clear in your explanation, I just have to look it over and learn by doing. LINQPad will probably be an invaluable tool for me, so I thank you very much for cluing me in to that. And thanks for taking the time to reply to me. This is important to me, so it means a lot!
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 39611716
As i said, glad to help :)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question