如何使用LinqToSql选择列AS

时间:2014-03-24 10:47:22

标签: vb.net linq-to-sql

嗨我有一个用户控件,它有一个转发器,它依赖于用户控件中的参数设置绑定到两个表之一。这两个表有类似的数据,所以在过去我使用了SQL连接并得到了这样的数据..

If m_Type = "STD" Then
   'Type is Standard so get data from S_Updates
   SQL="SELECT S_USER as User, S_Date as UDate, S_Update as Update FROM S_Updates WHERE S_Ref = " & m_caseRef & " ORDER By S_Date"
Else
   'Type is BSM so get data from B_Updates
   SQL="SELECT B_Device as User, B_Date as UDate, B_Update as Update FROM B_Updates WHERE B_Ref = " & m_caseRef & " ORDER By B_Date"
End If

然后我将结果绑定到我的转发器,该转发器具有带有以下语法的三个数据绑定容器的布局

<%= DataBinder.Eval(Container.DataItem, "UDate" %>
<%= DataBinder.Eval(Container.DataItem, "User" %>
<%= DataBinder.Eval(Container.DataItem, "Update" %>

我现在被要求更改代码,以便它使用LinqToSql

我有以下内容将从S_Updates中选择

Dim CaseUpdate = From u in dc.S_Updates
                 Where u.S_Ref = m_CaseRef
                 Order By u.S_Date
                 Select u

如何改变这一点,以便我得到......

CaseUpdate.UDate instead of CaseUpdate.S_Date, 
CaseUpdate.User instead of CaseUpdate.S_User and
CaseUpdate.Update instead of CaseUpdate.S_Update

1 个答案:

答案 0 :(得分:0)

好了解决了......

Dim CaseUpdate = From u in dc.S_Updates
                 Where u.S_Ref = m_CaseRef
                 Order By u.S_Date
                 Select New With {.UDate = u.S_Date,
                                  .User = u.S_User,
                                  .Update = u.S_Update}