通用清单项目减法问题 [英] Generic List items Subtraction problem
问题描述
对不起
现在
更新了----
大家好,请给我解决这个问题的方法
我有两个通用列表,这些列表使用GetAll函数从两个类中获取数据
我通过使用LINQ从两个类类型的通用列表中提取了两个字段.
LINQ结果集在两个列表中都有两个字段,我需要从它们中获取数据.
相同的列.
monthId,
金额
第一个列表具有学生每月明智费用的主要记录
而第二名则具有该学生当月的优惠记录明智的优惠费
喜欢
List1包含
MonthId |金额
1 | 1000
4 | 500
7 | 1000
10 | 500
清单2包含类似
的记录
MonthId |金额
1 | 100
4 | 50
所以我需要得到像
这样的减法结果
MonthId |金额
1 | 900
4 | 450
7 | 1000
10 | 500
请给出解决方案,我如何执行此操作..
我应该怎么做才能得到结果集.
LINQ可以帮助我尝试但在另一个var列表中未得到正确结果的结果
请给我一个惊喜
thnx
Sorry
now
Updated ----
hi all please give me the solution for this problem
i have two generic list having Data from two classes using GetAll Functions
i extracted two fields by using LINQ from both Class type Generic List.
LINQ Resultset have two fields in both Lists from them i need to data .
same columns .
monthId ,
Amount
first list have main records for a student month wise fee
while second have concessional record for that student month wise Concessional fee
like
List1 contains
MonthId | Amount
1 | 1000
4 | 500
7 | 1000
10 | 500
And List 2 Contains records like
MonthId | Amount
1 | 100
4 | 50
So i need to get Subtracted results like
MonthId | Amount
1 | 900
4 | 450
7 | 1000
10 | 500
Please give solution how i perform this ..
What should i do to get Result Set .
LINQ can help i tried but did not get right result in another var list
please give me a sloution
thnx
推荐答案
以下代码可用于获取问题中指定的结果集
The following code can be used to obtain the result set specified in the question
void Main()
{
List<fee> list1 = new List<fee> {
new Fee(1,1000),new Fee(4,500),
new Fee(7,1000),new Fee(10,500)
};
List<fee> list2 = new List<fee> {
new Fee(1,100),new Fee(4,50)
};
var resultSet = list1.Select (fee =>
new Fee(fee.MonthId, fee.Amount -
list2.FirstOrDefault (f => f.MonthId==fee.MonthId).Amount)
).ToList();
}
public struct Fee{
public int MonthId;
public decimal Amount;
public Fee(int monthId, decimal amount){
MonthId = monthId;
Amount = amount;
}
}
//resultSet is List<fee>
//MonthId Amount
// 1 900
// 4 450
// 7 1000
//10 500
注意:
IEnumerable
接口的FirstOrDefault
扩展方法将返回满足谓词或项目默认值的第一个找到的值.在这种情况下,Fee
结构为值类型,因此当list2
中不存在MonthId
时,我们将获得默认项目的Amount = 0
.
如果Fee
是类,则当list2
中不存在MonthId
时,将返回null
,因为reference type
的默认值为null.在这种情况下,我们需要先检查null
,然后再从list1
的金额中减去amount
.
Note:
The FirstOrDefault
extension method of IEnumerable
interface returns the First found value satisfying the predicate or the default value of the item. In this case the Fee
struct is Value type, hence we get Amount = 0
for the default item, when the MonthId
is not present in list2
.
If the Fee
is a class, then when the MonthId
is not present in list2
, null
will be returned, as the default value for reference type
is null. In such case we have check for null
, before deducting the amount
from list2
from the amount of list1
.
问候;
除了VJ Reddy的解决方案之外,我认为以下方法的扩展方法也可能是您的选择.对每个对象引用使用LINQ查询来调用扩展方法,这些对象引用在foreach()循环中具有待处理的更新:
(注意:以下代码段是用 LINQPad - www.linqpad.net 编写的>)
Greetings;
In addition to VJ Reddy''s solution, I think an extension method along the following lines might be an option for you as well. The extension method is called using a LINQ query for each of the object references that has an update pending inside a foreach() loop:
(Note: The following code fragments were written in LINQPad -www.linqpad.net)
class MonthlyAmount
{
public int MonthID {get; set;}
public decimal Amount {get; set;}
public MonthlyAmount(int Month, decimal Amount)
{
this.MonthID = Month;
this.Amount = Amount;
}
};
static class Extension
{
public static void Update<t>(this T Item, Action<t> UpdateAction)
{
UpdateAction(Item);
}
};
void Main()
{
List<monthlyamount> monthlyAmountsList = new List<monthlyamount>();
if (monthlyAmountsList == null)
{
return;
}
monthlyAmountsList.Add(new MonthlyAmount (1, 1000));
monthlyAmountsList.Add(new MonthlyAmount (4, 500));
monthlyAmountsList.Add(new MonthlyAmount (7, 1000));
monthlyAmountsList.Add(new MonthlyAmount (10, 500));
monthlyAmountsList.Dump();
List<monthlyamount> monthlyUpdatesList = new List<monthlyamount>();
if (monthlyUpdatesList == null)
{
return;
}
monthlyUpdatesList.Add(new MonthlyAmount (1, 100));
monthlyUpdatesList.Add(new MonthlyAmount (4, 50));
monthlyUpdatesList.Dump();
foreach(MonthlyAmount monthlyUpdate in monthlyUpdatesList)
{
(from entry in monthlyAmountsList
where entry.MonthID == monthlyUpdate.MonthID
select entry).First().Update(updatedEntry => updatedEntry.Amount -= monthlyUpdate.Amount);
}
monthlyAmountsList.Dump();
return;
}
并且列表<>在执行更新扩展之前,包含:
monthAmountsList List< MonthlyAmount> (4个项目)
月ID金额
1 1000
4 500
7 1000
10 500
monthUpdatesList List< MonthlyAmount> (2个)
月ID金额
1100
4 50
更新列表的内容.在foreach()循环之后将如下所示:
monthAmountsList List< MonthlyAmount> (4个项目)
月ID金额
1900
4450
7 1000
10 500
我希望这对您有所帮助.
最好的问候...
And the Lists<> before the Update Extension is executed contain:
monthlyAmountsList List<MonthlyAmount> (4 items)
MonthID Amount
1 1000
4 500
7 1000
10 500
monthlyUpdatesList List<MonthlyAmount> (2 items)
MonthID Amount
1 100
4 50
The contents of the Updated List<> after the foreach() loop would be as follows:
monthlyAmountsList List<MonthlyAmount> (4 items)
MonthID Amount
1 900
4 450
7 1000
10 500
I hope this was of help and interest to you.
Best Regards...
list1.forech(item => {
for (int i=0; i<=list2.count;i++)
if(item.monthid == list2[i].monthid)
item.amont -= list2[i].monthid;
};)
这篇关于通用清单项目减法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!