如何在mvc C#中加入和选择表数据?

时间:2012-11-07 09:47:04

标签: c# linq

在我的项目中,我有服务。 因此,我希望连接表并希望选择多个表数据。

所以我写了这句话。

var query1 = from opv in _opvRepository.Table
                         join o in _orderRepository.Table on opv.OrderId equals o.Id
                         join g in _graduandRepository.Table on opv.graduand_id equals g.graduand_id
                         join pv in _productVariantRepository.Table on opv.ProductVariantId equals pv.Id
                         join p in _productRepository.Table on pv.ProductId equals p.Id
                         where (opv.ceremony_id == ceremony_id) &&
                         (!o.Deleted) && (opv.IsHireItem == true)  &&
                         (!p.Deleted) &&
                         (!pv.Deleted) && (opv.ceremony_id == ceremony_id)
                         select opv,g;

但是有错误,如果我写opv and g.,我就无法选择select opv;。但我想选择两个表。 我该怎么办?

1 个答案:

答案 0 :(得分:1)

尝试使用匿名类型,即。

query1 = from opv in _opvRepository.Table
                         join o in _orderRepository.Table on opv.OrderId equals o.Id
                         join g in _graduandRepository.Table on opv.graduand_id equals g.graduand_id
                         join pv in _productVariantRepository.Table on opv.ProductVariantId equals pv.Id
                         join p in _productRepository.Table on pv.ProductId equals p.Id
                         where (opv.ceremony_id == ceremony_id) &&
                         (!o.Deleted) && (opv.IsHireItem == true)  &&
                         (!p.Deleted) &&
                         (!pv.Deleted) && (opv.ceremony_id == ceremony_id)
                         select new { table1Val = opv,
                                      table2Val = g 
                                     };