英文:
filter a list inside a class in .net
问题
我有这个类模型:
public record AllTransactionsResponse
{
public List<TransactionItem> Item { get; init; }
}
public record TransactionItem
{
public string FundCode { get; init; }
public FundTransactionResponse IssuanceItem { get; init; }
public FundRedemptionTransactionResponse RedemptionItem { get; init; }
}
我想筛选一些带有FundCode的记录:
var allTransactions = new AllTransactionsResponse();
allTransactions = allTransactions.Item.Where(i => i.FundCode != "11277").ToList();
但我得到了转换错误,我知道问题是什么,我应该将值转换为目标模型,但我需要最快的方法来做到这一点:
无法隐式转换类型'System.Collections.Generic.List<easy.fund.api.Application.Queries.TransactionItem>' 到 'easy.fund.api.Application.Queries.AllTransactionsResponse'
英文:
I have this class models :
public record AllTransactionsResponse
{
public List<TransactionItem> Item { get; init; }
}
public record TransactionItem
{
public string FundCode { get; init; }
public FundTransactionResponse IssuanceItem { get; init; }
public FundRedemptionTransactionResponse RedemptionItem { get; init; }
}
I want to filter some records with FundCode :
var allTransactions = new AllTransactionsResponse();
allTransactions = allTransactions.Item.Where(i => i.FundCode!="11277").ToList();
But I get convert error ,I know what is the problem and I should convert the value to target model,But I need the fastest way to do this :
Cannot implicitly convert type 'System.Collections.Generic.List<easy.fund.api.Application.Queries.TransactionItem>' to 'easy.fund.api.Application.Queries.AllTransactionsResponse
答案1
得分: 2
这里有一些问题。
如果你看这段代码:
allTransactions.Item.Where(i => i.FundCode != "11277").ToList();
你会发现它返回一个 IEnumerable<TransactionItem>
,然后你将其转换成了一个 List<>
。
但你期望的类型是 AllTransactionsResponse
。所以你需要将其赋值给这个类中的 Item 属性:
allTransactions.Item = allTransactions.Item.Where(i => i.FundCode != "11277").ToList();
但接着你会遇到另一个问题。你已将该属性声明为:
public List<TransactionItem> Item { get; init; }
所以你不能在后面再进行赋值。
因此,你需要将其改为 { get; set; }
类型,或者使用其他方法来解决这个问题。
由于你将结果重新赋值给原始项,你可以简单地使用 RemoveAll
来移除它们:
int numberRemoved = allTransactions.Item.RemoveAll(delegate (TransactionItem item) { return item.FundCode.Equals("11277"); });
这样你就不需要改变属性的声明,而且清楚地表达了你的实际目的。
英文:
There are a couple of issues here.
If you look at this code
allTransactions.Item.Where(i => i.FundCode != "11277").ToList();
you will see that it returns an IEnumerable<TransactionItem>
which you then convert into a List<>
But you expect a type of AllTransactionsResponse
. So you need to assign it to your Item property within this class
allTransactions.Item = allTransactions.Item.Where(i => i.FundCode != "11277").ToList();
But then you will have another problem. You have declared this property as
public List<TransactionItem> Item { get; init; }
So you can't assign it later.
So then you need to either change this to be of type { get; set; }
or use some other method to do this.
As you are assigning the result back to the original item, then you could simply remove then using RemoveAll
int numberRemoved = allTransactions.Item.RemoveAll(delegate (TransactionItem item) { return item.FundCode.Equals("11277"); });
This way you don't need to change your property declaration and it is clearer what you are actually trying to achieve.
答案2
得分: 1
我将使用 RemoveAll
方法来获取除了没有 FundCode
为 11277 的项目之外的项目,就像这样 allTransactions.Item.RemoveAll(i => i.FundCode == "11277");
。
英文:
I would use RemoveAll
method to get the items other then not having FundCode
as 11277. like thisallTransactions.Item.RemoveAll(i => i.FundCode == "11277");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论