views:

1228

answers:

2

I currently have a list that contains the following

CountryCode (string)
CountryStr  (string)
RegionStr   (string)
RegionID    (int)
AreaStr     (string)
AreaID      (int)

This is a flattened set of linked data (so basically the results of a joined search that ive stored)

The MVC route will only pass one string which I then need to match up to the data at the right level in the heirachy. So I'm trying to query the CountryStr then if it doesn't produce results the region then the area; but I need to do that bit of the query and for instance...

 var datURL = (from xs in myList
               //query 1
               where xs.RegionStr == rarREF
               select new
               {
                regionID = xs.RegionId,
                CountryID = xs.CountryCd
               }
               //IF theres no results 
               where xs.AreaStr == rarREF
               select new
               {
                AreaID = xs.AreaID
                regionID = xs.RegionId,
                CountryID = xs.CountryCd
               }             
               ).ToList();

The only way I see of doing this at the moment is running each query separately then checking which returned values and using that one. I'm hoping there's a cleverer, cleaner method.

+1  A: 

Aren't you looking for or operator? Does this not generate the results you want?

var datURL = (from xs in myList
              where xs.RegionStr == rarREF || xs.AreaStr == rarREF
              select new
              {
               AreaID = xs.AreaStr == rarREF ? xs.AreaID : default(int)
               regionID = xs.RegionId,
               CountryID = xs.CountryCd
              }).ToList();
çağdaş
+1  A: 

It won't be very easy to read, but you could do this in a single pass using something like this:

var datURL = (from xs in myList
              where xs.RegionStr == rarREF || xs.AreaStr == rarREF
              select new
              {
                AreaID = (xs.AreaStr == rarRef ? xs.AreaID : default(int)),
                RegionID = xs.RegionId,
                CountryID = xs.CountryId
              }
             ).ToList();

It might also be easier to read the query if it's rewritten slightly:

var datURL = (from xs in myList
              let isArea = xs.AreaStr == rarREF
              let isRegion = xs.RegionStr == rarREF
              where isRegion || isArea
              select new
              {
                AreaID = (isArea ? (int?)xs.AreaID : null),
                RegionID = xs.RegionId,
                CountryID = xs.CountryId
              }
             ).ToList();

If we save the comparison result, we can reuse it later. I also added a cast to int? to show how you could use a nullable value instead of using 0 as your "no Area" value.

dahlbyk
Swapping null for default(int) as in çağdaş seems to kind of do what Id expect. So is the where statement saying if it returns no results try AreaStr as I think it is? and with that I'm guessing I could easily work my way along adding more :op
Chris M
We're assuming that a Region string will never match an Area string, so the where will either return the list item where the Region matches or the Area matches. Then in the select we check again if the match is an Area or a Region. I'll edit with a version that might be easier to read.
dahlbyk
I'm quite impressed at what can be achieved with this linq lark. pretty darn sweet.Thanks for the help!
Chris M