求一对一的查询语句

wzllin111 2020-05-05 11:13:35

public class Student
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public StudentAddress studentAddress { get; set; }
}

public partial class StudentAddress
{
[Key, ForeignKey("Student")]
public int Studentid { get; set; }
public string City { get; set; }
public string Zipcode { get; set; }
public string Country { get; set; }
public Student Student { get; set; }
}



要求查询出所有Student的信息
...全文
144 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2020-05-06
  • 打赏
  • 举报
回复
var studentinfo = (from x in student
join y in studentaddress on x.id equals y.studentid
where x.id == xxx).select(z => new { student = z.x, studentaddress = z.y }).first()
正怒月神 2020-05-06
  • 打赏
  • 举报
回复
不用一对一查询。 直接 var stu=Student.First(x=>id==1); 如果要用到 第一个address地址。 那么就是 stu.Address.First();
wzllin111 2020-05-06
  • 打赏
  • 举报
回复
引用 1 楼 贵阳老马马善福专业维修游泳池堵漏防水工程 的回复:
var studentinfo = (from x in student join y in studentaddress on x.id equals y.studentid where x.id == xxx).select(z => new { student = z.x, studentaddress = z.y }).first()
报错了
泉低调前行 2020-05-06
  • 打赏
  • 举报
回复
var studentinfo = from x in student join y in studentaddress on x.id equals y.studentid where x.id == xxx Select new ViewModel(可以把你显示字段放进去){ .里面写出你想显示的字段 };

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧