开发者

LINQ - How to change value in select or foreach loop?

IQueryable items = from rr in _dt.AllItems
                   where rr.ID == ItemID
                   select new{rr.Item, rr.SecondItem}开发者_运维百科;

SecondItem is returning false or true. Is it possible to replace with different string values (for example with On and Off) and add to the items (IQueryable)?


You could do something like this:

IQueryable items = from rr in _dt.AllItems
               where rr.ID == ItemID
               select new {
                   rr.Item,
                   SecondItem = rr.SecondItem.HasValue ? "Off" : rr.SecondItem ? "On" : "Off"
               };


You mean like

  var items = from rr in _dt.AllItems 
              where rr.ID == ItemID 
              select new
              {
                  rr.Item, 
                  rr.SecondItem == true ? "On" : "Off"
              };


OK, I found out. I'm using in my SQL database column a bit type (true or false). When I‘m selecting with LINQ I need to replace with the ("On" or "Off") values. The code below meets my needs

IQueryable items = from rr in _dt.AllItems
               where rr.ID == ItemID
               select new {
                   rr.Item,
                   SecondItem = rr.SecondItem.Value ? "On" : "Off"
               };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜