Solved

LINQ to SQL

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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