Solved

MVC dropdownlist is showing duplicate options

Posted on 2013-10-29
6
549 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

719 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