Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return Type for Collection from LINQ Query

I have a method, that returns a group of accounts

Public Shared Function GetAllNotesByUser(ByVal UserID As Guid) As Account (??)
    Using db As New MyEntity
        Dim query= (From A In db.Account _
                    Where A.aspnet_Users.UserId = UserID _
                    Select A)
        Return query
    End Using
End Function

I would then like to pass this to another function to calculate the totals for all the accounts in the collection. Is it best practice to return an Ienumerable, a generic list, I'm just not sure what works best with LINQ and the entity framework.

like image 725
SIvart Ningvi Avatar asked Oct 15 '10 16:10

SIvart Ningvi


People also ask

What is the return type of a LINQ query?

By default, LINQ queries return a list of objects as an anonymous type. You can also specify that a query return a list of a specific type by using the Select clause.

What does group by return in LINQ?

A LINQ query can end with a GroupBy or Select clause. The result of GroupBy operators is a collection of groups. For example, GroupBy returns IEnumerable<IGrouping<TKey,Student>> from the Student collection: Return type of GroupBy()

Which LINQ method returns a Boolean value?

The All method of System. LINQ. Queryable class returns a Boolean value if all the elements of the sequence satisfy the provided condition. It returns true if all the elements satisfy the condition otherwise it returns false.

Does LINQ Select return new object?

Another option is to use LINQ's Select method. Normally, all we ask the Select method to do is return the object that will make up the new collection -- in fact, the Select method insists that the lambda expression passed to it return an object.


1 Answers

When propagating LINQ query results from methods in this manner, the best choice for the return type is IEnumerable(Of T) for LINQ to objects or IQueryable(Of T) for other LINQ providers. In this case it looks like Account is the type so IEnumerable(Of Account) or IQueryable(Of T) depending on the query type in question.

like image 105
JaredPar Avatar answered Nov 15 '22 04:11

JaredPar