Home > Enterprise >  entity framework join tables c#
entity framework join tables c#

Time:01-19

 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 >
                            <h3><a href="#"><%# Eval("namebook")%></a></h3>
                            <h3><a href="#"> ژانرکتاب:<%# Eval("title")%>0</a></h3>

                            <div >
                                <div >
                                    <p ><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

CodePudding user response:

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();
  •  Tags:  
  • Related