• Status: Solved
  • Priority: Medium
  • Security: Private
  • Views: 198
  • 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?

   //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

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

Open in new window

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

CamilliaAuthor Commented:
oh, let me see
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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