将Linq中的两个表连接到SQL [英] Join two tables in Linq to SQL

查看:91
本文介绍了将Linq中的两个表连接到SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许是一个很容易的问题,但我是在Linq到SQL的新。我有两个表

Maybe a quite easy question but I'm new in Linq to SQL. I have two tables

User : UserId,name,Password,Email
USER_TABLE: Id, UserId, FirstName,LastName......

我想要一个查询,例如: userID = 3 然后给所有的细节(FirstName,LastName等)
如何连接这两个表?如果可能,我宁愿 C#代码!

I would like a query that gives for example: userID=3 then give all the details (FirstName,LastName etc) How can I join these two tables? I would prefer C# code if it is possible!

推荐答案

不必自己做所有的管道。如果你在数据库中有正确的外键,你就不必加入自己。

You do not have to do all the plumbing yourself. If you have the right foreign keys in the database you will not have to join yourself.

你可以这样做:

var query = from u in db.Users
select new { User = u;
FirstName = u.UserTables.FirstName }

这篇关于将Linq中的两个表连接到SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆