如何连接多对多关系并在linq中包含其他表(lambda表达式)

时间:2016-10-22 21:09:03

标签: c# entity-framework linq lambda

我不知道如何将表连接到多对多关系并包含其他表,最后创建viewmodel。

我的图表enter image description here

最后我想拥有viewmodel:

 public class InsurancePolicyItemViewModel : InsurancePolicyItem
    {
    public InsurancePolicyItemViewModel()
    {
    }

    public InsurancePolicyItemViewModel(InsurancePolicyItem item)
    {
      //do something - supplement the basic information
    }

    public int InsurancePolicyItemId { get; set; }
    public string Name { get; set; }
    [...]
    public IEnumerable<Customers> { get; set; }
    public IEnumerable<InsurancePolicyItemInstallments> { get; set; }
   }

请帮我创建linq查询。使用lambda表达式会很好吗?

现在我的查询看起来像:

var result = contex.InsurancePolicyItem.Where(w => w.IsActive && w.IsVisible)
                                  .Include(i => i.InsuranceCompany)
                                  .Include(i => i.InsuranceCompanyPolicyStatus)
                                  .Include(i => i.InsuranceCompanyPolicyType)
                                  .Include(i => i.User_AddedBy)
                                  .Include(i => i.User_ModifiedBy)
                                  .Include(i => i.InsurancePolicyItemInstallment)
                                  .Include(i => i.InsurancePolicyItemFile)
                                  .Include(i => i.InsurancePolicyItemCustomers)
                                  .Select(s => s).ToList();

但它返回不包含Customers表的InsurancePolicyItem。

我试过了:

 var query = contex.InsurancePolicyItem.Where(w => w.IsActive && w.IsVisible).Select(s => new InsurancePolicyItemViewModel(s) {
                CustomerList = ????,
                InstallmentList = s.InsurancePolicyItemInstallment
                [...]
            });

但我不知道如何使用Join和include

2 个答案:

答案 0 :(得分:0)

为您提供一个连接不同表并在运行时创建新模型的示例

var persons = (
               // taking Persons table as c
               from c in context.Persons
               // now taking PersonStatus table as x and making join
               // with same key columns of x and c 
               join x in context.PersonStatus on c.TcKimlik equals x.Tckn
               join h in hospitals on x.HospitalCode equals h.KURUM_KODU
               where x.Statu == true
               //create new model by taking data of different tables of join
               select new DataViewModel
               {
                   Id = c.Id,
                   TcKimlik = c.TcKimlik,
                   Uyruk = c.Uyruk,
                   Ad = c.Ad,
                   Soyad = c.Soyad,
                   Cinsiyet = c.Cinsiyet,
                   DogumTarihi = c.DogumTarihi,
                   KurumStatu = h.PAYDAS,
                   KurumKodu = h.KURUM_KODU,
                   KurumAdi = h.KURUM_ADI,
                   BranchName = c.Brans.BranchName,
                   AcademicTitleName = c.AkademikUnvan.Title,
                   ManagerialTitleName = c.IdariUnvan.Title,
                   StaffStatuName = c.Durum.Statu,
                   BranchTypeName = c.Unvan.Type,
                   ServiceClassName = c.Unvan.ServiceClass.Name,
                   City = h.KURUM_ILI,
                   CityCode = h.IL_KODU,
                   CityTownName = h.KURUM_ILCESI
               }).AsQueryable();

我认为这有助于您解决问题

答案 1 :(得分:0)

我的解决方案基于您的代码:

  var a = contex.InsurancePolicyItem.Where(w => w.IsActive && w.IsVisible && w.ItemCreatedStatusId != Helpers.InsurancePolicyItemStatus.Draft)
                                                       .Select(s => new InsurancePolicyItemViewModel(s)
                                                       {
                                                           CustomerList = s.InsurancePolicyItemCustomers.Join(contex.Customers,
                                                                                 q => q.CustomersId,
                                                                                 c => c.CustomersId,
                                                                                 (q, c) => new
                                                                                 {
                                                                                     customerList = c
                                                                                 }).Select(ss => ss.customerList),
                                                           InstallmentList = s.InsurancePolicyItemInstallment,
                                                           FileList = s.InsurancePolicyItemFile,
                                                       });

但它返回错误

  

LINQ中仅支持无参数构造函数和初始值设定项   到实体

我发现了类似的问题:

Only parameterless constructors and initializers are supported in LINQ to Entities

但它不起作用...如果我创建匿名类型而不是InsurancePolicyItemViewModel,查询返回错误

  

无效的列名称'Discriminator'。

相关问题