Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-11-13
7
Medium Priority
?
497 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
[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
  • 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 1200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

722 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