isischen
asked on
Is possible to use 'If..Then' inside a LINQ query?
I have a typed dataset datatable from which I want to retrieve some column base on the column -- voc.
I wonder if I can use some like this --
For i = 0 to tiu.rows.count
dim thquery=From f IN Th IF Where f.Filed(Of String)("voc")= tiu.Rows(i) IS NUll then
dim tjquery=from f in Th where f.Field(of string)("voc") = tiu.Row(i+1)
End If Select New With {.v =f.Field(of string)("voc"), ....}
next
Is possible to enclose LINQ query inside a 'For...Next' and stuff a 'If..then' inside a LINQ query?
I wonder if I can use some like this --
For i = 0 to tiu.rows.count
dim thquery=From f IN Th IF Where f.Filed(Of String)("voc")= tiu.Rows(i) IS NUll then
dim tjquery=from f in Th where f.Field(of string)("voc") = tiu.Row(i+1)
End If Select New With {.v =f.Field(of string)("voc"), ....}
next
Is possible to enclose LINQ query inside a 'For...Next' and stuff a 'If..then' inside a LINQ query?
Dim tiu As DataTable = ds.Tables(tblname)'ds is untyped dataset
Dim dp As New dpDataSet()'dpDataSet is the typed dataset
Dim Th As DataTable = dp.Tables("th")
Dim thquery = From f In Th Where f.Field(Of String)("voc") = tiu.Rows(0).ToString Select New With {.v = f.Field(Of String)("voc"), .g = f.Field(Of String)("gP"), .s = f.Field(Of String)("subHead"), .ke = f.Field(Of String)("key")}
I am not a VB.NET developer but you can definitly achieve an IF/THEN construct if you use the following construct (C#):
var thquery = from f in Th
where f.Name == (c == "house") ? c : String.Empty
select f;
Here is a reference: http://www.blog.activa.be/2008/03/11/ifElseIfElseIfElseAnAlternative.aspx
Sorry for giving you in C# - maybe the VB construct is the the same ?:
var thquery = from f in Th
where f.Name == (c == "house") ? c : String.Empty
select f;
Here is a reference: http://www.blog.activa.be/2008/03/11/ifElseIfElseIfElseAnAlternative.aspx
Sorry for giving you in C# - maybe the VB construct is the the same ?:
ASKER
I can't use ? in vb.net 2008 express
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.dennydotnet.com/post/2008/02/Generating-a-Case-Statement-in-LINQ-to-SQL.aspx