Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ的案例和IIF_C#_Asp.net_Linq_Sql Server 2012 - Fatal编程技术网

C# LINQ的案例和IIF

C# LINQ的案例和IIF,c#,asp.net,linq,sql-server-2012,C#,Asp.net,Linq,Sql Server 2012,如何在ASP.Net中的LINQ中获得与以下SQL查询相同的结果 万一 select *, case when gender = 1 then 'Male' else 'Female' end as gen from info 而在iif中 select iif(gender = 1, 'Male', 'Female') as gen, * from info 我试着使用建议,但抛出了一个错误 InfoDataContext d

如何在
ASP.Net
中的
LINQ
中获得与以下
SQL
查询相同的结果

万一

select *, case 
        when 
            gender = 1 then 'Male' else 'Female' end 
        as gen from info
而在iif中

select iif(gender = 1, 'Male', 'Female') as gen, * from info 
我试着使用建议,但抛出了一个错误

InfoDataContext db = new InfoDataContext();

            var data = from y in db.infos
                       db.infos.Select(i => new info{info = i, i.gender == 1 ? "Male" : "Female"}).ToList()
                       select y;

            GridView1.DataSource = data;
            GridView1.DataBind();

还有一点是,第一个查询将新结果作为表中的最后一列返回,第二个查询作为表中的第一列返回。如何将
gen
列重新排列或替换为
gender

只需使用三元运算符:

var data = context.info.Select(i => new {info = i, gender = i.gender == 1 ? "Male" : "Female"}).ToList();

我已经接受了你的建议

InfoDataContext db = new InfoDataContext();
            var dat = db.infos.Select(i => new {info = i,i.id, i.name, Gen= i.gender == true ? "Male" : "Female", i.city});
            GridView1.DataSource = dat;
            GridView1.DataBind();

`InfoDataContext db=新的InfoDataContext();var data=从db.infos db.infos.Select(i=>newinfo{info=i,i.gender==1?“Male”:“Female”})中的y开始。ToList()选择y;GridView1.DataSource=数据;GridView1.DataBind()`@QadeerMangrio您的语法错误删除
new info{
并替换为
new{info=i..
}我对答案进行了编辑,以明确在将结果分配给数据变量之前不需要任何其他select或so。您的LINQ完全混乱,因为抛出了错误。您的最终结果必须是-
var data=db.infos.select(I=>new{info=I,I.gender==1?“Male”:“Female”})。ToList()
仍然存在错误。声明的匿名类型成员无效