Solved

linq where OR syntax?

Posted on 2011-02-23
7
724 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
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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