LinqToSql查询而不是一些SQL查询

时间:2012-07-02 17:20:05

标签: c# linq-to-sql

SELECT * FROM register WHERE user_id LIKE 'a%'

SELECT * FROM register WHERE user_id LIKE '%m'

SELECT * FROM register WHERE user_id LIKE '%andru%'


SELECT R.name,C.country_name,S.state_name 
FROM register R JOIN country C ON R.country_id=C.country_id
JOIN state S  ON R.state_id=S.state_id

SELECT R.name,C.country_name,S.state_name 
FROM register R INNER JOIN country C ON R.country_id=C.country_id       
INNER JOIN state S ON R.state_id=S.state_id

现在我需要 LinqToSql 查询而不是这些查询

1 个答案:

答案 0 :(得分:1)

var result = context.Registers.Select(x => x.StartsWith(foo)).ToList();

result = context.Registers.Select(x => x.EndsWith(foo)).ToList();

result = context.Registers.Select(x => x.Contains(foo)).ToList();

result = from register in context.Registers
         join state in context.States on register.state_id equals state.state_id
         select new { register.name, state.country_name, state.state_name }

注意,inner joinjoinSQL中的功能相同 - 因此无需复杂化。