Go Premium for a chance to win a PS4. Enter to Win

x
?
SolvedPrivate

Doing Greater Than or Equal to In LINQ

Posted on 2014-04-01
2
Medium Priority
?
67 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 64

Accepted Solution

by:
Fernando Soto earned 2000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

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 …
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

782 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