代码之家  ›  专栏  ›  技术社区  ›  Vinay Pandey

如何从多个表访问数据

  •  0
  • Vinay Pandey  · 技术社区  · 14 年前

    如何编写LINQ查询以访问多个表中的数据。如何编写以下SQL查询的LINQ查询:

    "select * from user,employee where user.Name='smith' and employee.company='xyz'"
    
    3 回复  |  直到 14 年前
        1
  •  1
  •   abatishchev Karl Johan    14 年前

    这样就可以了。

    var q = from u in db.user
            from e in db.employee
            where u.Name == "smith" && e.company == "xyz"
            select new
            {
               User = u,
               Employee = e
            };
    
        2
  •  0
  •   Joel Martinez    14 年前

    这取决于您使用的LINQ提供者。例如,假设您正确定义了外键,实体框架将创建它们所称的“导航属性”。因此,您可以用这种方式轻松地编写一个类似上面所述的LINQ查询:

    var query = data.Where(employee => employee.Name == "Smith" && employee.Company.Name == "xyz");
    
        3
  •  0
  •   Justin Niessner    14 年前
    var result = from u in context.User
                 from e in context.Employee
                 where (u.Name == "smith" && e.company == "xyz")