Solved

LINQ to SQL

Posted on 2013-06-06
5
371 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
  • 3
5 Comments
 
LVL 9

Assisted Solution

by:Aeriden
Aeriden earned 200 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 62

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 62

Accepted Solution

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

Expert Comment

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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

786 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