Solved

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

Posted on 2013-11-13
7
495 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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…
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.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

630 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