1

I am trying to get the result based upon the values of variables temp_DestinationGroupName, temp_CountryName and temp_RateTypeId.

If any of these variables are null or 0, it should not be included in where clause and rest statements should work and bring the result. I am getting zero rows using this query. Kindly suggest me something with these conditions in where clause. i tried following solutions on stackoverflow but still not getting the desired result.

var Rows = _CustomerRatesList.Where(w => (w.Id != rates.Id)
 && (w.DestinationGroupName == temp_DestinationGroupName || temp_DestinationGroupName!= null)
 && (w.CountryName == temp_CountryName || temp_CountryName!=null)
 && (w.RateTypeId == temp_RateTypeId || temp_RateTypeId !=0));
5
  • Are these queries against a database? If so, please expand your question, i.e. which ORM you use etc. The translation of expression trees into SQL by the LINQ provider is massively implementation-specific. Right now your code is apparently translated into inner joins. You may want to try to put the null checks in front. Commented Apr 29, 2015 at 7:18
  • Please give us an example. each of the && condition check executes only if previous ones return true. Commented Apr 29, 2015 at 7:19
  • no i have this query in c#(Visual Studio). and _CustomerRatesList is a list containing these values. Commented Apr 29, 2015 at 7:22
  • var Rows = _CustomerRatesList.Except(w => (w.DestinationGroupName == temp_DestinationGroupName || temp_DestinationGroupName!= null) && (w.CountryName == temp_CountryName || temp_CountryName!=null) && (w.RateTypeId == temp_RateTypeId || temp_RateTypeId !=0)).Where(w => w.Id != rates.Id); Commented Apr 29, 2015 at 7:24
  • if i change && into || i am getting all the records of list excluding the Id check, its working, but null values check is not working Commented Apr 29, 2015 at 7:24

3 Answers 3

2
(w.DestinationGroupName == temp_DestinationGroupName || temp_DestinationGroupName!= null)

Your condition here will return true as long as your input variable temp_DestinationGroupName is not null. I don't think that's what you had in mind.

Sign up to request clarification or add additional context in comments.

5 Comments

If this condition is true and others are false, only this should bring the result, but i am getting all the rows
You have the same issue in all of your conditions.
Yes same issue in all these conditions
...so you're never filtering out any results, so you're getting all the rows
No em not getting any record since i used && and if i change this to ||, whole list is showing, no filtering
1

You can split in many Where to make your request more readable :

var Rows = _CustomerRatesList.Where(w => w.Id != rates.Id)
                             .Where(w => w.DestinationGroupName != temp_DestinationGroupName || temp_DestinationGroupName != null)
                             .Where(w => w.CountryName != temp_CountryName || temp_CountryName != null)
                             .Where(w => w.RateTypeId != temp_RateTypeId || temp_RateTypeId !=0);

Notice that you must use != in first conditioon of all Where, so you'll get all data except when it's null. In your logic, may be the conditions will be == :

var Rows = _CustomerRatesList.Where(w => w.Id != rates.Id)
                             .Where(w => w.DestinationGroupName == temp_DestinationGroupName || temp_DestinationGroupName == null)
                             .Where(w => w.CountryName == temp_CountryName || temp_CountryName == null)
                             .Where(w => w.RateTypeId == temp_RateTypeId || temp_RateTypeId ==0);

Each Where result is an IEnumerable, Rows will be a IEnumerable. If you want a List, just add .ToList()

1 Comment

Thank you, i wrote my where conditions in separate lines, perfect !
0

I got the solution to this problem, now i am getting filtered result. Thank you all for your time :)

var otherRows = _CustomerRatesList.Where(w => w.Id != rates.Id)
                         .Where(w => w.DestinationGroupName == temp_DestinationGroupName || temp_DestinationGroupName == null)
                         .Where(w => w.CountryName == temp_CountryName || temp_CountryName == null)
                         .Where(w => w.RateTypeId == temp_RateTypeId || temp_RateTypeId == 0);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.