Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MVC dropdownlist is showing duplicate options

Posted on 2013-10-29
6
Medium Priority
?
587 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
Technology Partners: 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

597 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