我可以用LINQ写这样的东西:
var selection = from person in personList let initials = person.FirstName[0] + person.LastName[0] select initials;
我可以做类似于sql的事情,例如:
SELECT @Initials FROM [Person] SET @Initials = SUBSTRING (Person.FirstName,1,1) + SUBSTRING (Person.LastName,1)
可能不是,但也许有一个技巧?
我需要一个预先计算的变量,以便在复杂的WHERE子句中进一步使用,以避免极端的复杂性和代码重复.
解决方法
一个干净的方法,而不添加一个临时表,循环等,将使用通用表表达式(CTE).例:
;WITH PersonsWithInitials AS ( SELECT SUBSTRING (COALESCE(Person.FirstName,''),1) + SUBSTRING (COALESCE(Person.LastName,1) AS Initials,FirstName,LastName,City FROM [Person] ) SELECT FirstName,City,Initials FROM PersonsWithInitials WHERE /* Complex WHERE clause goes here and it can reference Initials as if it were a field */
代替上面的“空”,你可以使用一段时间或其他东西来代替空名字段.