Solved

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

Posted on 2013-11-13
7
492 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Import Data from Multiple Text Files in Excel 12 58
How come this regular expression allows "e"? 14 45
vb.net and creating a class 5 20
Testing of the web services 1 32
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 …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 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