如何在实体框架中加入时基于列来检索不同的行? [英] How to retrive distinct rows based on a column when join in entity framework?

查看:70
本文介绍了如何在实体框架中加入时基于列来检索不同的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加入2个表并根据实体框架中的1列检索不同的行。



我已经阅读了网上的帖子,包括SO。



在下面的代码中,

1.我根据 CurrentTurnID 键加入2个表格

2.我按照我想要的功能过滤表格

3.我通过设置所选功能的条件来过滤表格

4.我选择一列来计算总和所有它的价值



我只需要检索一次具有相同 CurrentTurnID 的记录。这是我的代码:



I want to join 2 tables and retrieve distinct rows based on 1 columns in entity framework.

I have read posts on web including SO.

In the code below,
1. I join 2 tables based on CurrentTurnID key
2. I filter the table by the features that I want
3. I filter the table by setting conditions on selected features
4. I select a column to calculate the sum of all it's value

I need to retrieve records that have the same CurrentTurnID just once. Here's my code:

int services = Convert.ToInt32(database.Tbl_CurrentTurn.AsNoTracking()
            .Join(database.Tbl_pay.AsNoTracking(), f => f.CurrentTurnID,
               s => s.CurrentTurnID, (f, s) => new
               {
                   f.CurrentTurnOfficialID,
                   f.CurrentTurnID,
                   f.SickID,
                   f.Remove,
                   f.Payment,
                   f.ServicePayment,
                   s.isDeposit
               })
            .Where(w => w.SickID == SickId && w.Remove != true && w.Payment == true && w.isDeposit != true)
            .Select(s => s.ServicePayment).DefaultIfEmpty(0).Sum());





如果我添加Distinct ()在Select()语句之后,它会考虑 ServicePayment 来过滤不同的行。



图像附加到我的Dropbox。第2和第3条记录具有相同的 CurrentTurnID 值。所以我想只考虑其中一个。然后总结 ServicePayment 列的记录。请注意,这2条记录中的其他列不同。



我尝试过的方法:



我已经阅读了网上的帖子。非解决方案帮助了我。



If I add Distinct() after Select() statement, it would consider the ServicePayment to filter distinct rows.

The Image is attached to my Dropbox. The 2nd and the 3rd records have the same CurrentTurnID value. So I want to consider just one of them. Then sum up records on ServicePayment column. Please notice that other columns are different in these 2 records.

What I have tried:

I have read posts on the web. Non of the solutions helped me.

推荐答案

尝试使用group by?如果值全部相同(或最小)
Try using group by? Then you can use max, if the values are all the same (or min)


这篇关于如何在实体框架中加入时基于列来检索不同的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆