문제

I am creating a result object from my query, something like this:

var result = from m in MyTable
             join r in some_more_tables
             select new ResultSummmary
             {
                  Description = m.Description,
                  start_date = r.start_dat
             };

But based on some condition before I get to this query and SELECT NEW, I want to be able to flex what I put in its Description filed, currently it is always m.Description but sometimes I want it to be a static text like "Hospital" and the rest of the times I want it to be m.Description as it is now.

How can we write it that way to be flexible?

도움이 되었습니까?

해결책

Let's pretend the condition is stored in a variable called condition. This would allow you to write the following

var result = from m in MyTable
         join r in some_more_tables
         select new ResultSummmary
         {
              Description = condition ?  m.Description : "Hospital",
              start_date = r.start_dat
         };

다른 팁

 select new ResultSummmary
             {
                  Description = someBool ? m.Description : "Hospital",
                  start_date = r.start_dat
             };
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top