Solved

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

Posted on 2013-11-13
7
490 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
Kaufmed
The first LINQ yielded all of the records

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

Expert Comment

by:käµfm³d 👽
Comment Utility
So you want one record from each group?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:CSUGDEN
Comment Utility
Yes. Would using DISTINCT be more appropriate?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 300 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

12 Experts available now in Live!

Get 1:1 Help Now