Solved

LINQ-select and LINQ-GroupBY-select returning different types

Posted on 2013-11-13
7
493 Views
Last Modified: 2016-02-10
Howdy
I am fairly new to all of this and plodding along with some modicum of succes using LINQ and MVC4 however I have run into a problem whereby

1. I successfully select from the Entity Framework using

 Dim lst1 = (From n In db.MenuDrivers Where n.MajorNode = "Applications" And n.MinorNode = "Driver" Select n)  

This returns a list of type: System.Data.Objects.ObjectQuery`1[Mvc.MenuDriver]"
of which I can pass to my layout as an View

2. I need to constrain the result and only send unique groups so I used

Dim lst = (From n In db.MenuDrivers Where n.MajorNode = "Applications" Group n By Key = n.MinorNode Into Group Select TotalGroups = Group)

which also works but it returns the lst as type System.Collections.Generic.List`1[System.Collections.Generic.IEnumerable`1[Mvc.MenuDriver]]

and I cannot pass this to the layout using the View any longer.
I get an error

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[System.Collections.Generic.IEnumerable`1[Mvc.MenuDriver]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Mvc.MenuDriver]'

How can I get the correct tyupe back from the group LINQ?

Regards
Charlie
0
Comment
Question by:CSUGDEN
  • 4
  • 3
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39645750
When you "group by", you are creating an IEnumerable of IEnumerables. So the question is now, what should the model contain? You've grouped by the key "n.MinorNode", so now you have an IEnumerable of groups, where each group has a different MinorCode. What are you wanting to extract from each group to pass to the model?
0
 

Author Comment

by:CSUGDEN
ID: 39645834
Kaufmed
The first LINQ yielded all of the records

I now want to cut that down base don a grouping of minornode
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39645854
So you want one record from each group?
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!

 

Author Comment

by:CSUGDEN
ID: 39645863
Yes. Would using DISTINCT be more appropriate?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39645905
Ah, then I would suggest using First instead of assigning the group directly.

e.g.

Dim lst = (From n In db.MenuDrivers
           Where n.MajorNode = "Applications"
           Group n By Key = n.MinorNode Into Group
           Select TotalGroups = Group.First())

Open in new window


That should yield you an IEnumerable(Of Mvc.MenuDriver).
0
 

Author Comment

by:CSUGDEN
ID: 39645954
I had a problem with the enumeration in the layout on the For Each where the error message suggested I use FirstOrDefault instead of First.

Why is that? DO you know?

Respond for your points.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 300 total points
ID: 39645965
You can use FirstOrDefault. FirstOrDefault can return null if there is nothing in the IEnumerable, but I don't think you would encounter that here. If you do, will need to add a Nothing check. (I don't think you'll need that, though.)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

679 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