在.NET中的类内过滤列表。

huangapple go评论76阅读模式
英文:

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&lt;TransactionItem&gt; 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 =&gt; i.FundCode!=&quot;11277&quot;).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 &#39;System.Collections.Generic.List&lt;easy.fund.api.Application.Queries.TransactionItem&gt;&#39; to &#39;easy.fund.api.Application.Queries.AllTransactionsResponse

答案1

得分: 2

这里有一些问题。

如果你看这段代码:

allTransactions.Item.Where(i =&gt; i.FundCode != &quot;11277&quot;).ToList();

你会发现它返回一个 IEnumerable&lt;TransactionItem&gt;,然后你将其转换成了一个 List&lt;&gt;

但你期望的类型是 AllTransactionsResponse。所以你需要将其赋值给这个类中的 Item 属性:

allTransactions.Item = allTransactions.Item.Where(i =&gt; i.FundCode != &quot;11277&quot;).ToList();

但接着你会遇到另一个问题。你已将该属性声明为:

public List&lt;TransactionItem&gt; Item { get; init; }

所以你不能在后面再进行赋值。

因此,你需要将其改为 { get; set; } 类型,或者使用其他方法来解决这个问题。

由于你将结果重新赋值给原始项,你可以简单地使用 RemoveAll 来移除它们:

int numberRemoved = allTransactions.Item.RemoveAll(delegate (TransactionItem item) { return item.FundCode.Equals(&quot;11277&quot;); });

这样你就不需要改变属性的声明,而且清楚地表达了你的实际目的。

英文:

There are a couple of issues here.

If you look at this code

allTransactions.Item.Where(i =&gt; i.FundCode != &quot;11277&quot;).ToList();

you will see that it returns an IEnumerable&lt;TransactionItem&gt; which you then convert into a List&lt;&gt;

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 =&gt; i.FundCode != &quot;11277&quot;).ToList();

But then you will have another problem. You have declared this property as

public List&lt;TransactionItem&gt; 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(&quot;11277&quot;); });

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 =&gt; i.FundCode == &quot;11277&quot;);

huangapple
  • 本文由 发表于 2023年7月23日 17:52:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定