Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Private
  • Views: 189
  • Last Modified:

Rewrite this SQL in LINQ - is this correct?

This works in SQL but I want  to write it in LINQ. Is this correct in LINQ?

LINQ
   //1. check if productId, PromotionId and Serial number exist 
            var p = (from s in _dataContext.Consumers
                where s.ProductId == model.ProductId &&
                      s.PromotionId == model.PromotionsId &&
                      s.ProductSerialNumber == model.SerialNumber
                select s);

            if (p != null) //row exists
                return false;

Open in new window


In SQL
if exists (select * from consumer where [ProductId] = @productId
                                         And [ProductSerialNumber] = @SerialNumber
										 and [PromotionId] = @PromotionId)
   return false

Open in new window

0
Camillia
Asked:
Camillia
1 Solution
 
käµfm³d 👽Commented:
I believe your LINQ is correct, however, I don't think your if will work. You need to actually execute the query. Try:

if (p.Any())

Open in new window

0
 
CamilliaAuthor Commented:
oh, let me see
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Tackle projects and never again get stuck behind a technical roadblock.
Join Now