Solved

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

717 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