SolvedPrivate

Doing Greater Than or Equal to In LINQ

Posted on 2014-04-01
2
49 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
[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 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…

739 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