简体   繁体   English

如何从与外键表一对一的关系中检索数据?

[英]How to retrieve data from one-to-one relationship with foreign key table?

I have one-to-one relationship and I insert in both of tables, but my problem is I can't retrieve data from foreign key table. 我有一对一的关系,并且在两个表中都插入了,但是我的问题是我无法从外键表中检索数据。 I want any solution. 我想要任何解决方案。

Qualified__For QF = new Qualified__For();
Int32? load = (from we in welding.Qualified__Fors
                    orderby we.Welder_ID
                    select (Int32?)we.Welder_ID).Max();

load = load.HasValue ? load.Value + 1 : 1;
int x = load.Value;

var load_Welder = welding.Qualified__Fors.SingleOrDefault(a => a.Welder_ID == x);

Lbl_from_material.Text = load_Welder.Material_From.ToString();
Lbl_to_material.Text = load_Welder.Material_To.ToString();

You are getting the max id then add 1 to it, so your second query doesn't return anything because there is no record exists that has a Welder_ID greater than maximum id.I think you should remove the load.Value + 1 您正在获取最大ID然后向其添加1 ,因此第二个查询不返回任何内容,因为不存在Welder_ID大于最大ID的记录,我认为您应该删除load.Value + 1

load = load.HasValue ? load.Value  : 1;

Also you can simplify it using null-coalescing operator like this: 您也可以使用null合并运算符来简化它,如下所示:

int x = load ?? 1;

What you have done is something like this (to make it more clear): 您所做的是这样的(更清楚地说):

int[] numbers = new [] { 1, 2, 3, 4, 5 };

int numberWhichIsNotExist = numbers.SingleOrDefault(x => x == numbers.Max() + 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM