将查询语法中的LINQ转换为lambda语法

时间:2013-01-20 00:41:30

标签: c# .net linq lambda

这是我的LINQ查询,请帮我改变此LINQ查询以使用Lambda表达式进行查询:

var query = from d in db.customers
            from s in db.tbl_states
            where d.cust_state == s.state_id
            select new
            {
                d.cust_name,
                s.state_name
            };

3 个答案:

答案 0 :(得分:2)

试试这个:

var query =
    db.customers.Join (         
        db.tbl_states,        
        d => d.cust_state,       
        s => s.state_id,   
        (d, s) => new { d.cust_name, s.state_name }
    );

答案 1 :(得分:2)

这应该这样做:

var query = db.customers.Join(db.tbl_states, d => d.cust_state, s => s.state_id, (d, s) => new { d.cust_name, s.state_name });

答案 2 :(得分:2)

我猜你的linq应该是这样的

var query = from d in db.customers
            join s in db.tbl_states on d.cust_state equals s.state_id
            select new
            {
                d.cust_name,
                s.state_name
            };

代表Lambda

var list = db.customers                     
.Join(db.tbl_states, 
      s => s.tbl_states, 
      d => d.state_id, 
      (s, d) => new                                                                                         
                 {
                     d.cust_name,
                     s.state_name
                 });