aruni
aruni

Reputation: 2752

How to join and select table data in mvc C#?

In my project I have services. So in side the service I want to join tables and want to select more than one table data.

So I write this cording.

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;

But there is error and I can't select opv and g. if I write select opv;it is ok. but i want to select both table. How can i do it??

Upvotes: 2

Views: 633

Answers (1)

activebiz
activebiz

Reputation: 6238

Try using anonymous types i.e.

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 
                                     };

Upvotes: 1

Related Questions