通过实体框架连接返回通用列表

问题描述 投票:0回答:1

我正在使用Entity Framework,并且希望联接2个表,然后将其作为列表返回。

但是我的方法返回List<contact>contact是我数据库中的一个表),我无法返回它。因为有了新对象,而不是像contact,我如何无法返回结果?

这是我的代码

public List<contact> GetAllContact()
{
     return db.contact.Where(c => c.deleted_at == null)
                      .Join(db.contact_image, contact => contact.id, image => image.contact_id, 
                            (contact, image) => new { 
                                                        contact.id,
                                                        contact.first_name,
                                                        contact.last_name,
                                                        contact.mobile,
                                                        contact.email,
                                                        contact.brithday,
                                                        contact.brithday_fa,
                                                        contact.created_at,
                                                        contact.updated_at,
                                                        contact.deleted_at,
                                                        image.image
                                                    }).ToList();
}
c# asp.net entity-framework generic-list
1个回答
0
投票
public class Contact_BO { public int ID {get;set;} public string First_Name { get; set; } public string Last_Name{ get; set; } public string Mobile { get; set; } }

当然,您可以添加所需的更多属性。然后像这样更改您的方法返回类型和您的linq查询

public List<Contact_BO> GetAllContact()
{
     return db.contact.Where(c => c.deleted_at == null)
                      .Join(db.contact_image, contact => contact.id, image => image.contact_id, 
                            (contact, image) => 
                                new Contact_BO { 
                                        ID = contact.id,
                                        First_Name = contact.first_name,
                                        Last_Name.last_name,
                                        Mobile = contact.mobile,
                                    }).ToList();
}
© www.soinside.com 2019 - 2024. All rights reserved.