0

Не могу понять в чем у меня проблема

contacts.aspx

<asp:Repeater ID="ReapeterContacts" runat="server"  >

    <HeaderTemplate>
        <table id="contact" class="table-bordered">
            <thead>
                <tr>
                    <th>ФИО</th>
                    <th>Компания</th>

                </tr>
            </thead>
    </HeaderTemplate>
    <ItemTemplate>

        <tr>
            <td>
                <b>
               <%# Eval("firstname")%></b>

            </td>
            <td>
                <%# Eval("company")%></b>

            </td>



        </tr>

    </ItemTemplate>
    <FooterTemplate>
        </table>

    </FooterTemplate>
</asp:Repeater>

contacts.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindTable();

    }


}




protected void BindTable() 
{

    using (contactsEntities db = new contactsEntities()) 
    {


        ReapeterContacts.DataSource = from p in db.persons
                                      join ph in db.phones on p.id equals ph.person_id into ljoin1
                                      from phone in ljoin1.DefaultIfEmpty()
                                      join e in db.emails on p.id equals e.person_id into ljoin2
                                      from mail in ljoin2.DefaultIfEmpty()
                                      select new
                                      {
                                          firstname = p.firstname,
                                          company = p.company
                                      };


        ReapeterContacts.DataBind();
    }

Выдает ошибку:

Additional information: Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to 

Я вроде и вот так сделал

ReapeterContacts.DataSource = (from p in db.persons
                              join ph in db.phones on p.id equals ph.person_id into ljoin1
                              from phone in ljoin1.DefaultIfEmpty()
                              join e in db.emails on p.id equals e.person_id into ljoin2
                              from mail in ljoin2.DefaultIfEmpty()
                              select new
                              {
                                  firstname = p.firstname,
                                  company = p.company
                              }).ToList();

Но ничего не помогает. Как мне в таблицу вывести данные из базы? Где я не прав?

1 ответ 1

1

А если вот так попробовать?

using (contactsEntities db = new contactsEntities()) 
{
    var result = from p in db.persons
                 join ph in db.phones on p.id equals ph.person_id into ljoin1
                 from phone in ljoin1.DefaultIfEmpty()
                 join e in db.emails on p.id equals e.person_id into ljoin2
                 from mail in ljoin2.DefaultIfEmpty()
                 select new
                 {
                    firstname = p.firstname,
                    company = p.company
                 };

   ReapeterContacts.DataSource = result.ToList();
   ReapeterContacts.DataBind();
}

UPDATE А зачем Вам такой сложный запрос если нужно только firstname и company? Может проще вот так

using (contactsEntities db = new contactsEntities()) 
{
    var result = from p in db.persons
                 select new
                 {
                    firstname = p.firstname,
                    company = p.company
                 };

   ReapeterContacts.DataSource = result.ToList();
   ReapeterContacts.DataBind();
}

Или так

using (contactsEntities db = new contactsEntities()) 
{
   var result = db.persons.Select(x=> new { x.firstname, x.company});

   ReapeterContacts.DataSource = result.ToList();
   ReapeterContacts.DataBind();
}

Или так (честно говоря не совсем в курсе как работает биндинг в ASP Webforms)

using (contactsEntities db = new contactsEntities()) 
{
   var result = db.persons.Select(x=> new {
                                             firstname = x.firstname,
                                             company = x.company
                                           });

   ReapeterContacts.DataSource = result.ToList();
   ReapeterContacts.DataBind();
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.