Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1984
  • Last Modified:

Selecting EntityCollection into collection using LINQ

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
pipelineconsulting
Asked:
pipelineconsulting
  • 3
1 Solution
 
BurniePCommented:
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
 
pipelineconsultingAuthor Commented:
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
 
pipelineconsultingAuthor Commented:
I seem to have solved the problem myself by adding .AsQueryable at the end of the LINQ statement
0
 
pipelineconsultingAuthor Commented:
Problem solved by myself after checking on MSDN
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now