SolvedPrivate

Doing Greater Than or Equal to In LINQ

Posted on 2014-04-01
2
48 Views
Last Modified: 2016-02-15
I am trying to create a LINQ expression in VB.NET.  For some reason, I cannot get it to work.

Here are the references I have at the top of my class:
Imports System.Data.SqlClient
Imports System.IO
Imports System.Text
Imports System.Linq

Open in new window

Here is the LINQ statement I have in one of my procedures:
        Dim results = From p In PreImportData.AsEnumerable
                        Join c In EnhancedACTRulesData
                        On p.Field(Of String)("GroupID") Equals c.Field(Of String)("GroupID") _
                        And p.Field(Of String)("SubGroupID") Equals c.Field(Of String)("SubGroupID") _
                        And p.Field(Of DateAndTime)("ServiceStartDate") >= c.Field(Of DateAndTime)("RuleStartDate")
                        Select p

Open in new window

It does not like the ">=" in my last condition.

If I remove this last condition, or change the ">=" to "Equals", it does not return any syntax errors.

I have searched a lot of code examples on the web, and see lots of uses of "=", ">=", and "<=", but it seems I cannot use any of those.  The only one it seems to like is "Equals".

Any idea why this is?  Am I missing something?

Thanks
0
Comment
Question by:JoeMiskey
2 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39969750
Hi JoeMiskey;

You can not use the And or >= operators within a Join clause you can only use Equals therefore you can only test for equality. Also if you need to test more then one field you need to use anonymous type and both anonymous types must have the same property names and must be in the same order so that the system sees them as the same anonymous types. Try the below code snippet to see if it returns the values wanted. Also the way you set the query is that it will return only the p data and not the c data as well so if you need both change the select to this, Select p, c

              
Dim results = From p In PreImportData.AsEnumerable
              Join c In EnhancedACTRulesData
                  On New With { .GroupID = p.Field(Of String)("GroupID"), .SubGroupID = p.Field(Of String)("SubGroupID") } Equals
                     New With { .GroupID = c.Field(Of String)("GroupID"), .SubGroupID = c.Field(Of String)("SubGroupID") }
              Where p.Field(Of DateAndTime)("ServiceStartDate") >= c.Field(Of DateAndTime)("RuleStartDate")
              Select p

Open in new window

0
 

Author Comment

by:JoeMiskey
ID: 39969828
I cannot believe I did that!  I had been staring at it for so long, I thought I was working in the WHERE clause, and not the JOIN clause (I had gone back-and-forth on the code a few times).

I changed it to this, and it worked perfectly.
        Dim results = From p In PreImportData.AsEnumerable
                        Join c In EnhancedACTRulesData
                        On p.Field(Of String)("GroupID") Equals c.Field(Of String)("GroupID") _
                        And p.Field(Of String)("SubGroupID") Equals c.Field(Of String)("SubGroupID") _
                        Where p.Field(Of Date)("ServiceStartDate") >= c.Field(Of Date)("RuleStartDate")
                        Select p

Open in new window

Thanks for finding my boneheaded error!
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Gridview 1 49
Help with error in Query 2 31
How to read text with RegEx... 2 26
PowerShell:  Column widths won't expand 3 23
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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 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