英文:
Filtering Records from List or Array
问题
我尝试在C#列表或数组中根据以下条件筛选记录 -
我们有唯一的发件人和多个接收者。
考虑将发件人值设为1,
- 如果发件人是1并且接收者也是2,以及发件人是2并且接收者是1,那么应该过滤掉这些记录。
- 它应该从上述筛选结果中取得最高的时间,并仅为每个接收者返回一条记录(发件人、接收者、时间和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,
- 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
- 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 :
"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"
}
]
Output:
"outcome": [
{
"sender": 2,
"receiver": 1,
"time": 2100,
"val": "This is Val2"
},
{
"sender": 1,
"receiver": 3,
"time": 2500,
"val": "This is Val4"
}
]
I have tried below but it is not working as expected:
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();
答案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 => (x.sender == 1 && x.receiver = 2) || (x.sender == 2 && 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 => y.time).FirstOrDefault(x => (x.sender == 1 && x.receiver = 2) || (x.sender == 2 && x.receiver == 1));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论