?
Solved

MVC dropdownlist is showing duplicate options

Posted on 2013-10-29
6
Medium Priority
?
568 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 2000 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
Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

764 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