简体   繁体   English

Linq查询的独特价值

[英]Distinct Value from Linq Query

I have the below class and linq query I am using to populate a grid ! 我有以下classlinq查询我用来填充grid

The Title is the same for every row returned. 返回的每一行的Title都是相同的。 What I am trying to do is populate mString with the distinct Title from the query so I can bind it to a seperate textblock. 我想要做的是使用查询中的distinct Title填充mString ,以便将其绑定到单独的文本块。

I probably didnt need to show all the code, but maybe it will help. 我可能不需要显示所有代码,但也许它会有所帮助。 How can I show the distinct Title . 如何显示distinct Title

public class Items
{
    public int Id { get; set; }
    public string Details { get; set; }
    public string Title { get; set; }
    public int NewNumber { get; set; }
}

private ObservableCollection<Items>  mItem = new ObservableCollection<Items>();
private string mString = string.Empty;

public string SpecTitle
{
    get { return mString; }
}

public ObservableCollection<Items> GetItems
{
    get { return mItem; }
}

Here is the linq query 这是linq查询

var results = (from z in mContext.View
               orderby z.ItemNumber ascending
               where z.ItemId == mId
               select new Items()
               {                                         
                   Id = z.ItemId,
                   Details = z.Details,
                   Title = z.ItemTitle,
                   NewNumber = z.ItemNumber
               });

List<Items> mNewItems = results.ToList();
mItem.Clear();
mNewItems.ForEach(y => mItem.Add(y));

Converting my comment into an answer: 将我的评论转换为答案:

just do Items.Select(x => x.Title).Distinct(); 只需做Items.Select(x => x.Title).Distinct(); .

var titleList = mNewItems.Select(i => i.Title).Distinct().ToList();

There is an additional library called moreLinq https://code.google.com/p/morelinq/ that has an extenction distinctby that you can you to distinct based on the given key. 还有一个名为moreLinq https://code.google.com/p/morelinq/的额外库,它具有明显的扩展性,您可以根据给定的密钥进行区分。

it would as simle as this 它就像这样

   var results = (from z in mContext.View
                                 orderby z.ItemNumber ascending
                                 where z.ItemId == mId
                                 select new Items()
                                 {                                         
                                     Id = z.ItemId,
                                     Details = z.Details,
                                     Title = z.ItemTitle,
                                     NewNumber = z.ItemNumber

                                 }).DistinctBy(c=>c.Title).ToList();

You can implement your custom comparer for distinct: 您可以为distinct执行自定义比较器:

public class ItemsComparer : IEqualityComparer<Items>
{
    public bool Equals(Items x, Items y)
    {
        return x.Title == y.Title;
    }

    public int GetHashCode(Items obj)
    {
        return obj.Title.GetHashCode();
    }
}

then just use 然后只是使用

var titleList = mNewItems.Distinct(new ItemsComparer()).Select(t=>t.Items);

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

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