简体   繁体   English

从类型注释列表中获得独特的价值

[英]getting distinct value from a list of type comment

    List<Comment> StreamItemComments = objStreamItem.GetComments();

... ...

    foreach (Comment Item in StreamItemComments)
        {
            if (ClientUser.UserName != Item.Sender)
            {
                Notification notificationObj = new Notification
                {
                    Sender = ClientUser.UserName,
                    Recipient = Item.Sender,
                    Value = "whatever value here",
                    TrackBack = "",
                    IsRead = false
                };
                notificationObj.Add();
            }
        }

What if there are two 'username' in List in Item.Sender. 如果Item.Sender中的列表中有两个“用户名”,该怎么办。 I'd like to send a notification once to the user. 我想向用户发送一次通知。 Here if there are duplicate usernames it will send two notifications because i am not filtering out duplicate Item.Senders from the list in StreamItemComments. 在这里,如果用户名重复,它将发送两个通知,因为我没有从StreamItemComments中的列表中过滤出重复的Item.Senders。

Consider writing a query to state your intentions. 考虑编写查询以说明您的意图。 You want the distinct senders of the item comments, but only where the sender is not the client user. 您希望项目注释的不同发件人,但仅在发件人不是客户用户的情况下。 Sounds like a query, does it not? 听起来像查询,不是吗?

var recipients = StreamItemComments
                    .Where(item => item.Sender != ClientUser.UserName)
                    .Select(item => item.Sender)
                    .Distinct();

You can then use this query to build your notifications 然后,您可以使用此查询来构建通知

foreach (var item in recipients)
{
    var notificationObj = new Notification
    {
         Sender = ClientUser.UserName,
         Recipient = item,
         ...
    }

    notificationObj.Add();
}

You could also fit this object construction into the query, as well, but with your .Add() invocation on each object, I left it out of the query. 您也可以将此对象构造也适合查询,但是通过对每个对象的.Add()调用,我将其排除在查询之外。 It wouldn't be difficult to incorporate, although you'd still need to loop over the output and invoke .Add() for each result. 合并起来并不难,尽管您仍然需要遍历输出并为每个结果调用.Add()

You can use a HashSet to determine if you already handled a username. 您可以使用HashSet来确定是否已经处理了用户名。

var set = new HashSet<string>();

foreach (var item in collection)
{
    if (set.Contains(item))
        continue;

    set.Add(item);

    // your notification code
}

For your concrete problem, set would contain usernames ( Item.Sender ). 对于您的具体问题, set将包含用户名( Item.Sender )。 So you may want to change the Add() argument. 因此,您可能需要更改Add()参数。

Use .Distinct() . 使用.Distinct() Since you can't use the default comparer, you can implement one like this 由于您无法使用默认比较器,因此可以实现这样的比较器

class MyEqualityComparer : IEqualityComparer<Comment>
{
    public bool Equals(Comment x, Comment y)
    {
        return x.Sender.Equals(y.Sender);
    }

    public int GetHashCode(Comment obj)
    {
        return obj.Sender.GetHashCode();
    }
}

And then just filter them like this. 然后像这样过滤它们。 You don't need the if statement. 您不需要if语句。

List<Comment> StreamItemComments = objStreamItem.GetComments()
    .Distinct(new MyEqualityComparer())
    .Where(x => x.Sender != ClientUser.UserName)
    .ToList();

What you can do is in the 你可以做的是

foreach ( Comment item in StreamItemComments)

add every notification to a Dictionary<user,msg> 将每个通知添加到Dictionary<user,msg>

and have another foreach key in Dictionary afterwards loop to send the actual message to user. 然后在Dictionary之后的循环中使用另一个foreach key将实际消息发送给用户。 This will ensure that only one message per user is sent out 这样可以确保每位用户仅发送一条消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM