help with linq query
-
I am trying to use linq...however running into the issue... how to I combine 3 fileds to Date?
.Dates = rows.Select(Function(r) r.Field(Of DateTime?)("DEIBMT") & "/" & ("DEIBDY") & "/" & ("DEIBCY") & ("DEIBYR"))
Dim data = myDataTable.AsEnumerable().GroupBy(Function(r) New With {Key .EEGRP = r.Field(Of String)("EEGRP"), Key .EESSN = r.Field(Of String)("EESSN")},
Function(key, rows) New With
{
Key .EEGRP = key.EEGRP,
Key .EESSN = key.EESSN,
.Dates = rows.Select(Function(r) _
r.Field(Of String)("DEIBMT")) _
.OrderByDescending(Function(d) d).Take(3).ToList()}
)
' ' .Dates = rows.Select(Function(r) r.Field(Of DateTime?)("DEIBMT") & "/" & ("DEIBDY") & "/" & ("DEIBCY") & ("DEIBYR")) _ -
I am trying to use linq...however running into the issue... how to I combine 3 fileds to Date?
.Dates = rows.Select(Function(r) r.Field(Of DateTime?)("DEIBMT") & "/" & ("DEIBDY") & "/" & ("DEIBCY") & ("DEIBYR"))
Dim data = myDataTable.AsEnumerable().GroupBy(Function(r) New With {Key .EEGRP = r.Field(Of String)("EEGRP"), Key .EESSN = r.Field(Of String)("EESSN")},
Function(key, rows) New With
{
Key .EEGRP = key.EEGRP,
Key .EESSN = key.EESSN,
.Dates = rows.Select(Function(r) _
r.Field(Of String)("DEIBMT")) _
.OrderByDescending(Function(d) d).Take(3).ToList()}
)
' ' .Dates = rows.Select(Function(r) r.Field(Of DateTime?)("DEIBMT") & "/" & ("DEIBDY") & "/" & ("DEIBCY") & ("DEIBYR")) _It depends what type your columns are. Assuming integers, something like this should work:
.Dates = rows.Select(Function(r) New DateTime( _
(100 * r.Field(Of Integer)("DEIBCY")) + r.Field(Of Integer)("DEIBYR"), _
r.Field(Of Integer)("DEIBMT"), _
r.Field(Of Integer)("DEIBDY")))
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
It depends what type your columns are. Assuming integers, something like this should work:
.Dates = rows.Select(Function(r) New DateTime( _
(100 * r.Field(Of Integer)("DEIBCY")) + r.Field(Of Integer)("DEIBYR"), _
r.Field(Of Integer)("DEIBMT"), _
r.Field(Of Integer)("DEIBDY")))
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
String representations of integers, or something else? If it's integers, then
Integer.TryParse
will work:Private Shared Function DateFromStringParts(ByVal century As String, ByVal year As String, ByVal month As String, ByVal day As String) As DateTime?
Dim cy, yr, mt, dy As Integer
If Not Integer.TryParse(century, cy) Then Return Nothing
If Not Integer.TryParse(year, yr) Then Return Nothing
If Not Integer.TryParse(month, mt) Then Return Nothing
If Not Integer.TryParse(day, dy) Then Return Nothing
Return New DateTime((100 * cy) + yr, mt, dy)
End Function
...
.Dates = rows.Select(Function(r) DateFromStringParts( _
r.Field(Of String)("DEIBCY"), _
r.Field(Of String)("DEIBYR"), _
r.Field(Of String)("DEIBMT"), _
r.Field(Of String)("DEIBDY")))
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
String representations of integers, or something else? If it's integers, then
Integer.TryParse
will work:Private Shared Function DateFromStringParts(ByVal century As String, ByVal year As String, ByVal month As String, ByVal day As String) As DateTime?
Dim cy, yr, mt, dy As Integer
If Not Integer.TryParse(century, cy) Then Return Nothing
If Not Integer.TryParse(year, yr) Then Return Nothing
If Not Integer.TryParse(month, mt) Then Return Nothing
If Not Integer.TryParse(day, dy) Then Return Nothing
Return New DateTime((100 * cy) + yr, mt, dy)
End Function
...
.Dates = rows.Select(Function(r) DateFromStringParts( _
r.Field(Of String)("DEIBCY"), _
r.Field(Of String)("DEIBYR"), _
r.Field(Of String)("DEIBMT"), _
r.Field(Of String)("DEIBDY")))
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
nice !! :-)