這裏是你會如何寫這個查詢的Lambda:
var customers = new List {
new Customer { CompanyId = 「AC」, CustomerId = 「Customer1」 },
new Customer { CompanyId = 「not-AC」, CustomerId = 「Customer2」 },
};
var userCustomers = new List {
new UserCustomer { CompanyId = 「AC」, CustomerId = 「Customer1」, User = 「not-admin」 },
new UserCustomer { CompanyId = 「AC」, CustomerId = 「Customer1」, User = 「admin」 },
new UserCustomer { CompanyId = 「AC」, CustomerId = 「Customer2」, User = 「not-admin」 },
new UserCustomer { CompanyId = 「AC」, CustomerId = 「Customer2」, User = 「admin」 },
new UserCustomer { CompanyId = 「not-AC」, CustomerId = 「Customer1」, User = 「not-admin」 },
new UserCustomer { CompanyId = 「not-AC」, CustomerId = 「Customer1」, User = 「admin」 },
new UserCustomer { CompanyId = 「not-AC」, CustomerId = 「Customer2」, User = 「not-admin」 },
new UserCustomer { CompanyId = 「not-AC」, CustomerId = 「Customer2」, User = 「admin」 }
};
使用查詢表達式
var query =
from c in customers
join uc in userCustomers on
new { c.CompanyId, c.CustomerId } equals new { uc.CompanyId, uc.CustomerId }
where c.CompanyId == 「AC」 && uc.User == 「admin「
select c;
使用Lambda表達式
var lambda = customers.Where(c => c.CompanyId == 「AC」) // inner sequence
.Join(userCustomers.Where(uc => uc.User == 「admin」), // outer sequence
c => new { c.CompanyId, c.CustomerId }, // inner key selector
uc => new { uc.CompanyId, uc.CustomerId }, // outer key selector
(c, uc) => c);
這兩種方法都會得到相同的結果(客戶的公司標識爲「AC」,客戶標識爲「Customer1」),但正如您所看到的,lambda表達式更難以書寫和閱讀!
希望這會有所幫助!
感謝您的鏈接Dzmitry ...備忘單是有用的...我已經看了101個樣本.. – RameshVel 2009-10-06 10:46:07
嘿我在哪裏可以得到101樣本的源代碼..任何想法..我couldnt找到那裏... – RameshVel 2009-10-06 11:49:20