Solved

MVC dropdownlist is showing duplicate options

Posted on 2013-10-29
6
510 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
Comment Utility
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
Comment Utility
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
Comment Utility
As i said, glad to help :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now