0

I have a linq query that gives me an System.Collections.Generic.IEnumerable(of <Anonymous Type>). Actually I wanted to create a class to get better access to what I want form that query but I fail aready with declaring a member variable that should hold the query outcome. The query I use in the Sub New() of my class is (explained and c# version here:)

Dim jan1 = New DateTime(DateTime.Today.Year, 1, 1)
Dim startOfFirstWeek = jan1.AddDays(1 - CInt(jan1.DayOfWeek))
Dim weeks = Enumerable.Range(0, 54).[Select](Function(i) New With { _
    Key .weekStart = startOfFirstWeek.AddDays(i * 7) _
}).TakeWhile(Function(x) x.weekStart.Year <= jan1.Year).[Select](Function(x) New With { _
    x.weekStart, _
    Key .weekFinish = x.weekStart.AddDays(4) _
}).SkipWhile(Function(x) x.weekFinish < jan1.AddDays(1)).[Select](Function(x, i) New With { _
    x.weekStart, _
    x.weekFinish, _
    Key .weekNum = i + 1 _
})

My Class should look like:

Public Class WeekInfo

Private _weeks as ?????

end Class

Could anyone tell me what is the usual procedure to accomplish that task and how to put the find a type for my member to access the weeks variable?

Community
  • 1
  • 1
ruedi
  • 5,365
  • 15
  • 52
  • 88

2 Answers2

0

VB.Net anonymous types are exposed as type Object so you're variable would be:

Dim weeks As IEnumerable(Of Object)

You will not get intellisense on the anonymous type's fields outside of the function you created them in but you can access them dynamically, i.e.

For Each week In weeks
    Console.WriteLine(week.weekStart + " " + week.weekFinish)
Next

If you need to expose the type externally you should define a concrete class or struct instead.

Phillip Trelford
  • 6,513
  • 25
  • 40
  • What i did is. I put the query in The constrctor, then i created a member _weeks as IEnumerable(of Object). puttint _weeks = weeks and try to access the _weeks in a property that gives me a Opstict On doesnt allow late binding error. Do you know how i can solve this? – ruedi Aug 20 '14 at 12:16
  • @ruedi you can only access the properties with Option Strict Off - see [VB.Net equivalent for C# 'dynamic' with Option Strict On](http://stackoverflow.com/a/2890023/2012417), otherwise you'll need to use a concrete type – Phillip Trelford Aug 20 '14 at 13:34
  • if i get that right you have to set Option Strict Off for the whole project in VB.NET but you can use dynamic for a specific variable in c# to set Option Strict Off just that variable? That would be a big + for c# concering working with linq objects! – ruedi Aug 20 '14 at 13:56
  • 1
    @ruedi it appears you can set [Option Strict Off](http://msdn.microsoft.com/en-gb/library/zcd4xwzs.aspx) on a per file basis – Phillip Trelford Aug 20 '14 at 15:22
0

Make your WeekInfo class look like this: Public Class WeekInfo

    Public Property WeekStart As Date
    Public Property WeekEnd As Date
    Public Property WeekNumber As Int32
End Class

And then load it with you linq query as list of your WeekInfo(s):

    Dim jan1 = New DateTime(DateTime.Today.Year, 1, 1)
    Dim startOfFirstWeek = jan1.AddDays(1 - CInt(jan1.DayOfWeek))
    Dim weeks = Enumerable.Range(0, 54).Select(Function(i) New With { _
        Key .weekStart = startOfFirstWeek.AddDays(i * 7) _
    }).TakeWhile(Function(x) x.weekStart.Year <= jan1.Year).[Select](Function(x) New With { _
        x.weekStart, _
        Key .weekFinish = x.weekStart.AddDays(4) _
    }).SkipWhile(Function(x) x.weekFinish < jan1.AddDays(1)).[Select](Function(x, i) New WeekInfo With { _
        .WeekStart = x.weekStart, _
        .WeekEnd = x.weekFinish, _
        .WeekNumber = i + 1 _
    }).ToList()
rfolt
  • 111
  • 5