简体   繁体   中英

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

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

Also you can simplify it using null-coalescing operator like this:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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