If Else Decisions in LINQ Query

I have a small LINQ query to populate a drop-down control (WinForms Telerik application) with data rows displaying two values ​​(ITNBR and description):

var query = from i in db.ItemMasts.AsNoTracking()
orderby i.ITNBR
select new { i.ID, cboText = i.ITNBR + " - " + i.Description };

//Now we can bind the results to the control
cbo.DataSource = query.ToList();
cbo.DisplayMember = "cboText";
cbo.ValueMember = "ID";

Which works great. But I want the user to be able to switch the order of "cboText" - as sometimes it is displayed as ITNBR - Description, and sometimes it is displayed as "Description" - ITNBR

Is there a way to do this without writing two separate LINQ queries? thanks in advance.

+4
source share
1 answer

, . , isUserflag , , false, .

 var query = from i in db.ItemMasts.AsNoTracking()
 orderby i.ITNBR
 select new { i.ID, cboText = isUserflag ? i.ITNBR + " - " + i.Description : i.Description + " - " +i.ITNBR };
+5

All Articles