Solved

LINQ to SQL

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How do I get the unique ID after insert? 87 92
asp.net mvc5 3 38
crm development 2 39
How can I use this extension method? 8 36
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now