2011-10-18 96 views
4

我有以下查詢,Linq中給出具體的名字列

var employees = from emp in this.DataWorkspace.v2oneboxData.me_employees 
       where emp.me_is_deleted.Value == false && emp.me_is_manager == true 
       select new{emp.me_first_name,emp.me_last_name,emp.me_pkey}; 

我想給一個特定的名稱,以emp.me_first_name列,就像我們在SQL查詢像做:

select emp_first_Name as "First Name" from me_employees 

如何在linq查詢中做到這一點???

也可以在Linq的select查詢中結合firstName和lastname嗎?就像我們在SQL查詢中所做的那樣:

select me_first_name + ' ' + me_last_name as 'Full Name' from me_employee 

我該如何在linq中實現這個任務?

感謝

回答

6

你不能讓它First Name,因爲這是一個不但您可以使用:

select new { FirstName = emp.me_first_name, 
      LastName = emp.me_last_name, 
      Key = emp.me_pkey }; 

基本上,匿名類型的屬性名稱只有默認爲,無論您使用哪種屬性值 - 您都可以自己指定它,但它必須是有效的C#標識符。

或爲組合:

select new { FullName = emp.me_first_name + " " + emp.me_last_name, 
      Key = emp.me_pkey }; 
2
from emp in this.DataWorkspace.v2oneboxData.me_employees 
where emp.me_is_deleted.Value == false && emp.me_is_manager == true 
select new{FullName = String.Format("{0} {1}",emp.me_first_name, emp.me_last_name),emp.me_pkey};