Solved

linq where OR syntax?

Posted on 2011-02-23
7
726 Views
Last Modified: 2012-05-11
Hi

I have the following code

however, every time it looks through it constructs the SQL to be

where ID =1 and ID= 2 and ID =3 ....
however, i need it to be OR:
where ID =1 OR ID= 2 OR ID =3 ....

how would i do this?


if (recordId.Length > 0)
                {

                    string[] csv = recordId.Split(new char[] { ',' });

                    foreach (string row in csv)
                    {
                        query = query.Where(s => s.MasterProductID == Convert.ToInt32(row));
                    }

                }
                else
                { query = query.Where(s => s.MasterProductID == 0); }

Open in new window

0
Comment
Question by:websss
[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
7 Comments
 

Author Comment

by:websss
ID: 34967573
Actually, just realised its not even building the where clause
its simply overwritting it

how do i get it to build a multiple where clause and also use OR keyword

thanks
0
 
LVL 9

Expert Comment

by:s_chilkury
ID: 34967586
Try following:
WHERE ID == 1 || ID == 2 || ID == 3
0
 

Author Comment

by:websss
ID: 34967604
s_chilkury
can you match your example wth my code please with the for loop
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 34967609
you can try like this
Where ID in (1,2,3)
0
 

Accepted Solution

by:
websss earned 0 total points
ID: 34967932
The answer i was looking for was:

  string[] csv = recordId.Split(',');

                var query = from p in dataContext.MTSubProducts
                                 where csv.Contains(Convert.ToString(p.MasterProductID))
                                 select p;
0
 
LVL 19

Expert Comment

by:Amandeep Singh Bhullar
ID: 35913564
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…

737 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