How to do If statement in Linq Query

爱⌒轻易说出口 提交于 2019-12-10 10:26:23

问题


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:


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.




回答2:


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();


来源:https://stackoverflow.com/questions/1153486/how-to-do-if-statement-in-linq-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!