Solved

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

Posted on 2008-06-23
4
962 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
Comment Utility
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
Comment Utility
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
Comment Utility
I can't use ? in vb.net 2008 express
0
 
LVL 55

Accepted Solution

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

iif(someConditionHere, result1, result2)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

17 Experts available now in Live!

Get 1:1 Help Now