tags:

views:

165

answers:

3
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)?

+1  A: 

You mean like

  var items = from rr in _dt.AllItems 
              where rr.ID == ItemID 
              select new
              {
                  rr.Item, 
                  rr.SecondItem == true ? "On" : "Off"
              };
R0MANARMY
Thank you guys for quick response. I tried both ways and I got the error "Cannot implicitly convert type 'System.Nullable<bool>' to 'bool'"? Do you know work around?
Eugene
See my edit - since SecondItem is nullable you first have to check if it has value (is not null) and then use it.
Marek Karbarz
@Marek Karbarz: You don't need to do a HasValue check since the Nullable struct overrides the Equals method (http://msdn.microsoft.com/en-us/library/c7b1e4z6%28v=VS.90%29.aspx).
R0MANARMY
+1  A: 

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"
               };
Marek Karbarz
Thank you it looks like it works. I am not sure because I realized that I am storing this value as a bit (0 or 1) for some reason its always returning false(even if its 1 or 0). Any ideas?
Eugene
OK, I found out. It works this way - (SecondItem = rr.SecondItem.Value ? "On" : "Off")
Eugene
@Eugene: Be careful about just doing rr.SecondItem.Value. The whole idea behind the Nullable<T> struct is that there may not be a value (highly unlikely in your case, since you're getting data from a bit field).
R0MANARMY
Ok I see, but in my case it will be false if not true and backward. Thank you
Eugene
@Eugene: it can be false, true, and **null**. That's why Marek Karbarz's example checks the HasValue property (in case it is null).
R0MANARMY
+1  A: 

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