entity framework 连接表 c#

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>
我想加入 table 书籍和 table 类别。 输出中没有显示标题。 enter image description here

可能是表之间的关系配置不正确。试试这个语法

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