简体   繁体   中英

entity framework join tables c#

 void load_data()
    {
        DatabaseContext context = new DatabaseContext();
        var query = context.books.Include("category").Select(p => new
        {
            p.id,
            p.namebook,
            p.Price,
            p.picture,
            p.categoiryid,
            title= p.category.title,
        }).OrderByDescending(p => p.id).Take(8).ToList();
        ListView1.DataSource = query;
        ListView1.DataBind();
    } }

 <div class="text py-3 pb-4 px-3 text-center"> <h3><a href="#"><%# Eval("namebook")%></a></h3> <h3><a href="#"> ژانرکتاب:<%# Eval("title")%>0</a></h3> <div class="d-flex"> <div class="pricing"> <p class="price"><span><%# Eval("price")%> تومان</span></p> </div>
I want to join the table books and table category. The title is not shown to me in the output. enter image description here

It is possible that relations between tables are not properly configured. Try this syntax

var query = (from p in context.Set<Book>()
            join c in context.Set<Category>()
                on p.categoryId equals c.id
            select new {    
            p.id,
            p.namebook,
            p.Price,
            p.picture,
            p.categoiryid,
            c.titel,
            }).OrderByDescending(p => p.id).Take(8).ToList();

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