?
Solved

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

Posted on 2008-06-23
4
Medium Priority
?
969 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

764 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