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

x
?
Solved

Selecting EntityCollection into collection using LINQ

Posted on 2010-11-20
4
Medium Priority
?
1,950 Views
Last Modified: 2012-05-10
I am trying to select a collection of entities into a custom class using the following code:

 
Public Function GetKeywordSummaries(ByVal _Keywords As EntityCollection(Of Keyword)) As System.Linq.IQueryable(Of KeywordSummary)

            Return (From k In _Keywords _
                        Order By k.Word _
                        Select New KeywordSummary With { _
                           .Id = k.Id, _
                           .Word = k.Word, _
                           .ArticleCount = k.Articles.Count})

        End Function

Open in new window


In the code, I have an entitycollection named Keywords and a custom class KeywordSummaries.

When executing the function, the following error occurs:

 
Unable to cast object of type 'WhereSelectEnumerableIterator`2[Pipeline.Blogjammer.Keyword,Pipeline.Blogjammer.KeywordSummary]' to type 'System.Linq.IQueryable`1[Pipeline.Blogjammer.KeywordSummary]'

Open in new window


Does anyone know why and how to get around this problem?
0
Comment
Question by:pipelineconsulting
[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
  • 3
4 Comments
 
LVL 16

Expert Comment

by:BurnieP
ID: 34180131
I'm not sure, maybe you need to specify what type you are returning.  Gave you an example.

Public Function GetKeywordSummaries(_Keywords As EntityCollection(Of Keyword)) As System.Linq.IQueryable(Of KeywordSummary)

	Return (From k In _Keywords
                 Order By k.Word 
                 Select New KeywordSummary With { _
		 .Id = k.Id, _
		 .Word = k.Word, _
		 .ArticleCount = k.Articles.Count }).[Single](Of System.Linq.IQueryable(Of KeywordSummary))

End Function

Open in new window

0
 

Author Comment

by:pipelineconsulting
ID: 34180301
This doesn't seem to work.  I am getting an IntelliSense error in Visual Studio:

"Overload Resolution failed because no accessible 'Single' accepts this number of type arguments"
0
 

Accepted Solution

by:
pipelineconsulting earned 0 total points
ID: 34180976
I seem to have solved the problem myself by adding .AsQueryable at the end of the LINQ statement
0
 

Author Closing Comment

by:pipelineconsulting
ID: 34211357
Problem solved by myself after checking on MSDN
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

704 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