Solved

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

Posted on 2008-06-23
4
963 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now