Solved

LINQ to SQL

Posted on 2013-06-06
5
372 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 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 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 63

Expert Comment

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

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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