c# - Linq select Item where it is equal to ID in another table -


i not sure how possible have 2 tables , want grab value table 2 via value of table 1.

table 1 has foreign key called "rank" int. table 2 has value called "name" string. table 1's "rank" correlates table 2's "id".

so when say

var result = db.table1.select(x => new { x.name, x.rank }).tolist(); //bob - 2

i want like

var result = db.table1.select(x => new { x.name, table2.rank.where(id == x.rank) }).tolist(); //bob - gold

i still new linq though , not sure how rank's string value other table within query this.

edit

tables using , relational values.

user: id (pk), s1elo (fk pastelos), champ (fk championlist), elo (fk elolist)

pastelo: id (pk), rank

championlist: id (pk), name

elolist: id (pk), rank

working example users , pastelo

var result = db.users.join(db.pasteloes, x => x.s1elo, y => y.id, (x, y) => new { y.rank, x.name, x.other_items_in_users }).tolist();

note: pastelo pasteloe's due ef making plural when synced db, why user users, think referred "context".

you try following:

var result = db.table1.join(db.table2,                              x=>x.rank,                              y=>y.id,                             (x,y) => new { x.rank, y.name }).tolist(); 

in above linq query make join between 2 tables, table1 , table2 based on association , select want.

another way try write query following:

var result = (from t1 in db.table1              join t2 in db.table2              on t1.rank equals t2.id              select new { t1.rank, t2.name, }).tolist(); 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -