简体   繁体   中英

Understanding associations in linq to sql

Im trying to understand how to use the associations(so that my linq statements are more readable)

I have a table logs, which has many mapping IDs. I have a table with a primary key mapping ID which has permutations of countryID, categoryID, and categoryTypeID. Each of those has that column as a primary key on each of there tables. My understanding is that if i set up my context right i could do something like

(from l in datacontext.logs
where l.mappings.category.barId== myNumber
select l)

But I cant seem to figure out how to make that happen. Is it possible, and how could i go about setting up my project to do this.

Can you not use

(from l in datacontext.logs
join m in datacontext.mappings on l.mappingId equals l.mappingid
join c in datacontext.category on m.categoryid equals c.categoryid
where c.barId == myNumber
select l).ToList();

I am making assumptions on your table structure based on your question.

I figured it out, my associations were backwards. logs is not the parent of mapings, rather mapings is the parent of logs.

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