Solved

Is possible to use 'If..Then' inside a LINQ query?

Posted on 2008-06-23
4
964 Views
Last Modified: 2013-11-26
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?                    
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")}

Open in new window

0
Comment
Question by:isischen
  • 2
4 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21850147
Yes, you can, with the ? operator, have a look to some examples:
http://www.dennydotnet.com/post/2008/02/Generating-a-Case-Statement-in-LINQ-to-SQL.aspx
0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 21850213
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 ?:
0
 

Author Comment

by:isischen
ID: 21871286
I can't use ? in vb.net 2008 express
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 21871754
oops sorry, I've provided a C# answer.
Instead of ? use the IIf() function:

iif(someConditionHere, result1, result2)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net - KeyPress Event 4 36
Disable extension 8 38
Obtain cell value using column ID in DataGrid using VB.NET 2 19
Convert datetime to time string 10 21
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question