开发者

Linq return a field or another

I have a typed dataset, this typed dataset has some fields : id, Code, Screen, NL, FR ..

I do linq query on it, ok.

But I'd like depending of the language return : id, Code and NL or id, Co开发者_如何学Cde and FR

I tried something like this (see below but not work).

var res = 
from p in dataTable.AsEnumerable()
where p.Screen.ToUpper() == "WWWWWW" && p.Control.ToUpper() == "OOOO"
select new {p.Id, p.Code, if(lg == "FR") ?? p.FR : p.NL };

Thanks,


Well, you can use the conditional operator for that if the two types are the same:

var res = from p in dataTable.AsEnumerable()
          where p.Screen.ToUpper() == "WWWWWW" && p.Control.ToUpper() == "OOOO"
          select new {p.Id, p.Code, Foo = lg == "FR" ? p.FR : p.NL };

Note how you have to name the property, as otherwise the C# compiler can't guess what you want.


You have to name the properties of an anonymous class if they aren't guessable by the compiler.

select new {p.Id, p.Code, Language = lg == "FR" ? p.FR : p.NL };

should work.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜