使用 LINQ 查找具有相同值的行
find rows with same value using LINQ
上图的问题在于
- 我想根据 TransLog_Date
向股东申请红股
- 因为第 1 行的股东已经将他的部分股份转让给第 3 行的持有人,所以他的新股份价值在第 2 行第 5 列显示为 100000
- 我希望根据第 2 行第 5 列而不是第 1 行第 5 列中的值计算红利份额。
我在 LINQ 中尝试了几个代码,但都出错了,因为值乘以所有 3 条记录,这是完全错误的。
var transQuery = (from tq in db.TransactionsLogDbSet
where (tq.TransactionType == TransactionType.PurchaseOfShares)
|| (tq.TransactionType == TransactionType.TransferOfShares)
|| (tq.TransactionType == TransactionType.BonusSharesDeclared)
select tq);
var query = from row in db.TransactionsLogDbSet
group row by row.Transholder_ID into g
select new { Count = g.Count() };
好的,我想我终于得到了你想要的。
对吗?:将一些 TransactionType
的所有行按 Transholder_ID
分组,然后从每组 select 中获得最大值 Translog_Date
的行,给出您是每个 Transholder_ID
的最新行。一旦你得到了结果,你就可以迭代它来计算你需要的任何东西。
from t in db.TransactionLogDbSet
where where tq.TransactionType == TransactionType.PurchaseOfShares || tq.TransactionType == TransactionType.TransferOfShares || tq.TransactionType == TransactionType.BonusSharesDeclared
group t by t.Transholder_ID into g
select (from t1 in g orderby t1.Translog_Date descending select t1).First();
编辑:只要我们最终得出正确的查询,我将继续编辑我的答案的以下部分。
根据您到目前为止的最后一条评论,您只想 select 具有 TransLog_Date
<= 给定 DateTime qualifiedDate
.
的所有行
from t in db.TransactionLogDbSet
where t.TransactionLog_Date <= qualifiedDate
select t
是吗?
上图的问题在于
- 我想根据 TransLog_Date 向股东申请红股
- 因为第 1 行的股东已经将他的部分股份转让给第 3 行的持有人,所以他的新股份价值在第 2 行第 5 列显示为 100000
- 我希望根据第 2 行第 5 列而不是第 1 行第 5 列中的值计算红利份额。
我在 LINQ 中尝试了几个代码,但都出错了,因为值乘以所有 3 条记录,这是完全错误的。
var transQuery = (from tq in db.TransactionsLogDbSet where (tq.TransactionType == TransactionType.PurchaseOfShares) || (tq.TransactionType == TransactionType.TransferOfShares) || (tq.TransactionType == TransactionType.BonusSharesDeclared) select tq); var query = from row in db.TransactionsLogDbSet group row by row.Transholder_ID into g select new { Count = g.Count() };
好的,我想我终于得到了你想要的。
对吗?:将一些 TransactionType
的所有行按 Transholder_ID
分组,然后从每组 select 中获得最大值 Translog_Date
的行,给出您是每个 Transholder_ID
的最新行。一旦你得到了结果,你就可以迭代它来计算你需要的任何东西。
from t in db.TransactionLogDbSet
where where tq.TransactionType == TransactionType.PurchaseOfShares || tq.TransactionType == TransactionType.TransferOfShares || tq.TransactionType == TransactionType.BonusSharesDeclared
group t by t.Transholder_ID into g
select (from t1 in g orderby t1.Translog_Date descending select t1).First();
编辑:只要我们最终得出正确的查询,我将继续编辑我的答案的以下部分。
根据您到目前为止的最后一条评论,您只想 select 具有 TransLog_Date
<= 给定 DateTime qualifiedDate
.
from t in db.TransactionLogDbSet
where t.TransactionLog_Date <= qualifiedDate
select t
是吗?