Thiago
Thiago

Reputation: 1555

linq - create a list of integers from a list of objects

Hy!

I'm trying to create a list of days(integer) from a list of dates(date).

I tryed to do this.....

Dim days As New List(Of Integer)
days = From a In DATES
                Where a.desc = "ferias"
                Select Day(a.date)

But Im getting an error saying that its

not possible to convert an object of type "WhereSelectListIterar"'2[dates, System.int32] in system.collections.generic.list'1[System.int32]

How can I get the integer days of "DATES", which is a list of Date?

Tks!

Upvotes: 3

Views: 5141

Answers (5)

Tratak
Tratak

Reputation: 168

Dim days as As List(Of Integer) = dates.ConvertAll(Of Integer)(Function(D As Date) D.Day)

Upvotes: 3

Jay
Jay

Reputation: 6017

You will want to do something like this:

  days = (From a In dates
                    Where a.desc= "ferias"
                    Select a.date.Day).ToList

You need to .ToList the result.

Upvotes: 1

Niranjan Singh
Niranjan Singh

Reputation: 18290

Convert it to list using ToList as:

Dim days As New List(Of Integer)
days = (From a In DATES
                Where a.desc = "ferias"
                Select Day(a.date)).ToList()

Upvotes: 1

Francis
Francis

Reputation: 3383

First of all, don't do Dim days As New List(Of Integer) if you intend to overwrite this value. Next, use the ToList() extension method if you want a list instead of an enumerable.

Dim days As List(Of Integer) = _
    (From a In DATES _
     Where a.desc = "ferias" _
     Select Day(a.date)).ToList()

Upvotes: 1

Greg B
Greg B

Reputation: 14888

Call ToList on the result of the Linq expression.

Upvotes: 0

Related Questions