从列表或数组中筛选记录

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

Filtering Records from List or Array

问题

我尝试在C#列表或数组中根据以下条件筛选记录 -
我们有唯一的发件人和多个接收者。
考虑将发件人值设为1,

  1. 如果发件人是1并且接收者也是2,以及发件人是2并且接收者是1,那么应该过滤掉这些记录。
  2. 它应该从上述筛选结果中取得最高的时间,并仅为每个接收者返回一条记录(发件人、接收者、时间和Val)。
    我的首选是使用lambda表达式进行过滤,并将其放入HashSet中以返回顶部时间值。
    例如,输入:
"secrets": [
    {
      "sender": 1,
      "receiver": 2,
      "time": 2000,
      "val": "This is Val1"
    },
    {
      "sender": 2,
      "receiver": 1,
      "time": 2100,
      "val": "This is Val2"
    },
    {
      "sender": 1,
      "receiver": 2,
      "time": 1500,
      "val": "This is Val3"
    },
    {
      "sender": 1,
      "receiver": 3,
      "time": 2500,
      "val": "This is Val4"
    }
    ]

输出:

"outcome": [
    {
      "sender": 2,
      "receiver": 1,
      "time": 2100,
      "val": "This is Val2"
    },
    {
      "sender": 1,
      "receiver": 3,
      "time": 2500,
      "val": "This is Val4"
    }
]

我尝试了下面的代码,但它的工作方式不如预期:

int senderId = 1;
for(int i=0; i<receivers; i++)
{
var senderSecrets = secrets.Where(x => (x.sender == senderId) && (x.receiver == receivers[i])).ToArray();
var receiverSecrets = secrets.Where(x => (x.sender == receivers[i]) && (x.receiver == senderId)).ToArray();
英文:

I am trying to filter records in C# list or array based on following conditions -
We have unique sender and multiple Receivers.
Considering Sender value as 1,

  1. If Sender is 1 and Receiver is 2 as well as Sender is 2 and Receiver is 1 then it should filter out those records
  2. It should take highest time from above filtered result and return only one record (Sender, Receiver, Time and Val) for each Receiver.
    My First preference is filtering using lambda expression and putting in HashSet to return top time value.
    For Example, Input :
&quot;secrets&quot;: [
    {
      &quot;sender&quot;: 1,
      &quot;receiver&quot;: 2,
      &quot;time&quot;: 2000,
      &quot;val&quot;: &quot;This is Val1&quot;
    },
    {
      &quot;sender&quot;: 2,
      &quot;receiver&quot;: 1,
      &quot;time&quot;: 2100,
      &quot;val&quot;: &quot;This is Val2&quot;
    },
    {
      &quot;sender&quot;: 1,
      &quot;receiver&quot;: 2,
      &quot;time&quot;: 1500,
      &quot;val&quot;: &quot;This is Val3&quot;
    },
    {
      &quot;sender&quot;: 1,
      &quot;receiver&quot;: 3,
      &quot;time&quot;: 2500,
      &quot;val&quot;: &quot;This is Val4&quot;
    }
    ]

Output:

&quot;outcome&quot;: [
    {
      &quot;sender&quot;: 2,
      &quot;receiver&quot;: 1,
      &quot;time&quot;: 2100,
      &quot;val&quot;: &quot;This is Val2&quot;
    },
    {
      &quot;sender&quot;: 1,
      &quot;receiver&quot;: 3,
      &quot;time&quot;: 2500,
      &quot;val&quot;: &quot;This is Val4&quot;
    }
]

I have tried below but it is not working as expected:

int senderId = 1;
for(int i=0; i&lt;receivers; i++)
{
var senderSecrets = secrets.Where(x =&gt; (x.sender == senderId) &amp;&amp; (x.receiver == receivers[i])).ToArray();
var receiverSecrets = secrets.Where(x =&gt; (x.sender == receivers[i]) &amp;&amp; (x.receiver == senderId)).ToArray();

答案1

得分: 0

Here's the translated code portion:

> 如果发送者是1且接收者是2,以及发送者是2且接收者是1,则应该过滤掉这些记录。

Secrets.Where(x => (x.sender == 1 && x.receiver == 2) || (x.sender == 2 && x.receiver == 1));

> 它应该从上面筛选的结果中获取最高时间,并仅返回每个接收者的一条记录(发送者、接收者、时间和值)。我首选使用 Lambda 表达式进行筛选,并将其放入 HashSet 中以返回最高时间值。

secrets.OrderBy(y => y.time).FirstOrDefault(x => (x.sender == 1 && x.receiver == 2) || (x.sender == 2 && x.receiver == 1));
英文:

> If Sender is 1 and Receiver is 2 as well as Sender is 2 and Receiver is 1 then it should filter out those records.

Secrets.Where(x =&gt; (x.sender == 1 &amp;&amp; x.receiver = 2) || (x.sender == 2 &amp;&amp; x.receiver == 1));

> It should take highest time from above filtered result and return only
> one record (Sender, Receiver, Time and Val) for each Receiver. My
> First preference is filtering using lambda expression and putting in
> HashSet to return top time value.

secrets.OrderBy(y =&gt; y.time).FirstOrDefault(x =&gt; (x.sender == 1 &amp;&amp; x.receiver = 2) || (x.sender == 2 &amp;&amp; x.receiver == 1));

huangapple
  • 本文由 发表于 2023年4月11日 12:41:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982460.html
匿名

发表评论

匿名网友

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

确定