Question

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?

Was it helpful?

Solution

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
         };

OTHER TIPS

 select new ResultSummmary
             {
                  Description = someBool ? m.Description : "Hospital",
                  start_date = r.start_dat
             };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top