简体   繁体   English

在Linq-to-SQL查询上需要帮助(也许是嵌套查询吗?)

[英]Need help on Linq-to-SQL query (maybe nested query?)

I am asking for your help regarding a linq query to SQL. 我正在寻求有关SQL的linq查询的帮助。

Here is a part of the diagram of my database: https://imgur.com/xFBUm3q 这是我的数据库图的一部分: https : //imgur.com/xFBUm3q

My problem is in the following relationship: tbl_607_bottle and tbl_607_gaz_reporting , I can have several reportings for a bottle but reporting can only have one bottle. 我的问题在于以下关系: tbl_607_bottletbl_607_gaz_reporting ,我可以为一个瓶子提供多个报告,但是报告只能有一个瓶子。

I do this request but it's not satisfying. 我执行此请求,但并不令人满意。

var GazReportingWizardViewModel = 
    (from gr in db.tbl_607_gaz_reporting  
     join a in db.tbl_607_actors on gr.FK_ID_actors equals a.id  
     join bo in db.tbl_607_bottle on gr.FK_ID_bottle equals bo.ID  
     join loc in db.tbl_607_location on bo.FK_ID_location equals loc.ID  
     join cc in db.tbl_607_conformity_certificate on bo.FK_ID_conformity_certificate equals cc.ID  
     join j in db.tbl_607_join_bottle_gaz on bo.ID equals j.FK_ID_bottle  
     join g in db.tbl_607_gaz on j.FK_ID_gaz equals g.ID  
     join od in db.tbl_607_order_details on g.FK_ID_order_details equals od.ID  
     where loc.ID == Process   
     select new GazReportingWizardViewModel  
                {  
                    bottle_conti_number = bo.bottle_conti_number,  
                    pressure_value = gr.pressure_value,  
                    reporting_date = gr.reporting_date,  
                    first_name = gr.tbl_607_actors.first_name,  
                    last_name = gr.tbl_607_actors.last_name,  
                    expiration_date = cc.expiration_date,  
                    content = od.content_comments  
                }).Distinct()
                  .OrderBy(t => t.reporting_date)
                  .ToList();  

I want the last report on each bottle but it return all reports on each bottle. 我想要每个瓶子的最新报告,但它返回每个瓶子的所有报告。 Would you have an idea of ​​the solution? 您对解决方案有想法吗?

Thank you for your time 感谢您的时间

Thank you so much! 非常感谢! I persisted in trying to solve the problem in a single query when the solution was so simple. 当解决方案如此简单时,我坚持尝试通过单个查询解决问题。

var GazReportingWizardViewModel = (from gr in db.tbl_607_gaz_reporting
                                   join a in db.tbl_607_actors on gr.FK_ID_actors equals a.id
                                   join bo in db.tbl_607_bottle on gr.FK_ID_bottle equals bo.ID
                                   join loc in db.tbl_607_location on bo.FK_ID_location equals loc.ID
                                   join cc in db.tbl_607_conformity_certificate on bo.FK_ID_conformity_certificate equals cc.ID
                                   join j in db.tbl_607_join_bottle_gaz on bo.ID equals j.FK_ID_bottle
                                   join g in db.tbl_607_gaz on j.FK_ID_gaz equals g.ID
                                   join od in db.tbl_607_order_details on g.FK_ID_order_details equals od.ID
                                   where loc.ID == Process 
                                   select new GazReportingWizardViewModel
                                   {
                                    bottle_conti_number = bo.bottle_conti_number,
                                    pressure_value = gr.pressure_value,
                                    reporting_date = gr.reporting_date,
                                    first_name = gr.tbl_607_actors.first_name,
                                    last_name = gr.tbl_607_actors.last_name,
                                    expiration_date = cc.expiration_date,
                                    content = od.content_comments
                                    }).ToList();

 var res = from element in GazReportingWizardViewModel
           group element by element.bottle_conti_number
           into groups
           select groups.OrderByDescending(p => p.reporting_date).First();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM