switch语句中的LINQ [英] switch statement in linq

查看:172
本文介绍了switch语句中的LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code使用LINQ SQL连接是:

My code for sql connection using linq is:

var query1 = from u in dc.Usage_Computers
          where u.DomainUser == s3
          select u; // selects all feilds from table

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

我有一个被称为操作的表格Domainuser具有像1,2,3值的字段。当我填充这些值数据网格,我想将它们转换为有意义的值一样,如果操作的值是1,那么在数据网格的登录显示,如果2然后在注销等等...

I have a field called "Operation" in the table "Domainuser" which has values like "1, 2, 3". When I populate these values to data grid I wanted to convert them to meaningful values like if the value of Operation is 1 then display in datagrid as "logon", if 2 then "logoff" etc...

我如何从数据库中检索后为它们分配值?

How do i assign values for them after retrieving from database?

推荐答案

这个技术似乎并不特别适用于您的问题,但在这里它是无论如何。

This technique does not seem particularly applicable to your problem, but here it is anyway.

您可以使用C#的创建一个LinqToSql的SQL case语句? 运营商。

You can create a SQL case statement in LinqToSql by using the C# ? : operator.

var query1 =
  from u in dc.Usage_Computers
  where u.DomainUser == s3
  select new {usage = u, 
    operation =
      u.DomainUser.Operation == 1 ? "login" :
      u.DomainUser.Operation == 2 ? "logoff" :
      "something else"
  };

这篇关于switch语句中的LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆