?
Solved

LINQ to SQL

Posted on 2013-06-06
5
Medium Priority
?
376 Views
Last Modified: 2013-06-10
Hello Experts,
i want to write LINQ for the following SQL in C#:

If Not EXISTS(Select * from OrderPayment
Where OrderHeaderId = 72884643 and TransactionTypeId = 52)

any idea?

Thanks
0
Comment
Question by:mani_sai
[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
  • 3
5 Comments
 
LVL 9

Assisted Solution

by:Aeriden
Aeriden earned 800 total points
ID: 39227196
var q = From op In db.OrderPayment Where op.OrderHeaderId == 72884643 && op.TransactionTypeId == 52
if (q.Any())
{
  // do something if there are records
}
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39227911
Hi mani_sai;

The following code snippet should do what you need. Note there are two options 1 if you expect to have returned a collection of records and the other if you will be returning one or non.

Dim ohId = 72884643
Dim ttId = 52

// This version is used if the result set returned from the query can hev more then one record.
Dim oPayment = (From o in DataContextInstance.OrderPayment
                Where o.OrderHeaderId = ohId And o.TransactionTypeId = ttId
                Select o).ToList()

If Not oPayment.Any() Then
    ' No Records Returned
End If
                
// This query is used if there can be only one record returned or no records returned.   
Dim oPayment = (From o in DataContextInstance.OrderPayment
                Where o.OrderHeaderId = ohId And o.TransactionTypeId = ttId
                Select o).SingleOrDefault()
                
If oPayment = Nothing Then
    ' No Records Returned
End If

Open in new window

0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 1200 total points
ID: 39227916
Hi mani_sai;

Sorry here it is in C#.

var ohId = 72884643;
var ttId = 52;

// This version is used if the result set returned from the query can hev more then one record.
var oPayment = (from o in DataContextInstance.OrderPayment
                where o.OrderHeaderId == ohId && o.TransactionTypeId == ttId
                select o).ToList();

if( !oPayment.Any())
{
    // No Records Returned
}
                
// This query is used if there can be only one record returned or no records returned.   
var oPayment = (from o in DataContextInstance.OrderPayment
                where o.OrderHeaderId == ohId && o.TransactionTypeId == ttId
                select o).SingleOrDefault();
                
if( oPayment == null )
{
    // No Records Returned
}

Open in new window

0
 
LVL 8

Author Closing Comment

by:mani_sai
ID: 39236034
Thanks
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39236664
Not a problem mani_sal, glad I was able to help.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

801 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