C# 使用LINQ选择列中所有不同的值

C# 使用LINQ选择列中所有不同的值,c#,linq,visual-studio-2012,csv,asp.net-web-api,C#,Linq,Visual Studio 2012,Csv,Asp.net Web Api,我在VS 2012中创建了一个Web Api。 我试图从一列“Category”中获取所有值,即所有唯一值,我不希望列表返回重复的值 我使用此代码获取特定类别的产品。如何获得类别的完整列表(类别列中的所有唯一值) public IEnumerable GetProductsByCategory(字符串类别) { 返回repository.GetAllProducts()。其中( p=>string.Equals(p.Category,Category,StringComparison.Ordin

我在VS 2012中创建了一个Web Api。 我试图从一列“Category”中获取所有值,即所有唯一值,我不希望列表返回重复的值

我使用此代码获取特定类别的产品。如何获得类别的完整列表(类别列中的所有唯一值)

public IEnumerable GetProductsByCategory(字符串类别)
{
返回repository.GetAllProducts()。其中(
p=>string.Equals(p.Category,Category,StringComparison.OrdinalIgnoreCase));
}

要拥有独特的类别:

var uniqueCategories =  repository.GetAllProducts()
                                  .Select(p=>p.Category)
                                  .Distinct();

简单易用

我必须找到具有以下详细信息的不同行 类别:史考恩特里
列:countryID、countryName、isactive
此字段中没有主键。我成功地完成了以下查询

public DbSet<SCountry> country { get; set; }
    public List<SCountry> DoDistinct()
    {
        var query = (from m in country group m by new { m.CountryID, m.CountryName, m.isactive } into mygroup select mygroup.FirstOrDefault()).Distinct();
        var Countries = query.ToList().Select(m => new SCountry { CountryID = m.CountryID, CountryName = m.CountryName, isactive = m.isactive }).ToList();
        return Countries;
    }
public DbSet country{get;set;}
公共列表DoDistinct()
{
var query=(从国家组m中的m通过新的{m.CountryID,m.CountryName,m.isactive}进入mygroup选择mygroup.FirstOrDefault()).Distinct();
var Countries=query.ToList().Select(m=>newscontry{CountryID=m.CountryID,CountryName=m.CountryName,isactive=m.isactive}).ToList();
返回国;
}

有趣的是,我在LinqPad中尝试了这两种方法,Dmitry Gribkov的变体使用组似乎更快。(此外,由于结果已经不同,因此不需要最终的不同

我的(有些简单)代码是:

public class Pair 
{ 
    public int id {get;set;}
    public string Arb {get;set;}
}

void Main()
{

    var theList = new List<Pair>();
    var randomiser = new Random();
    for (int count = 1; count < 10000; count++)
    {
        theList.Add(new Pair 
        {
            id = randomiser.Next(1, 50),
            Arb = "not used"
        });
    }

    var timer = new Stopwatch();
    timer.Start();
    var distinct = theList.GroupBy(c => c.id).Select(p => p.First().id);
    timer.Stop();
    Debug.WriteLine(timer.Elapsed);

    timer.Start();
    var otherDistinct = theList.Select(p => p.id).Distinct();
    timer.Stop();
    Debug.WriteLine(timer.Elapsed);
}
公共类对
{ 
公共int id{get;set;}
公共字符串Arb{get;set;}
}
void Main()
{
var theList=新列表();
var randomiser=新的Random();
对于(int count=1;count<10000;count++)
{
列表。添加(新的一对
{
id=随机发生器。下一个(1,50),
Arb=“未使用”
});
}
var timer=新秒表();
timer.Start();
var distinct=theList.GroupBy(c=>c.id).Select(p=>p.First().id);
timer.Stop();
Debug.WriteLine(timer.appeased);
timer.Start();
var otherDistinct=theList.Select(p=>p.id).Distinct();
timer.Stop();
Debug.WriteLine(timer.appeased);
}

我尝试了此操作,但出现生成错误:无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“System.Collections.Generic.IEnumerable”。存在显式转换(是否缺少强制转换?)@测试员看到King King对问题的评论太棒了,这对我来说很有效,我可以从重复的数据中选择一个不同的类别列表!!!非常感谢你的问题对我来说不清楚,当你
按类别获得产品时,当然你得到的
产品应该有相同的
类别,这里有什么不同?是的,此代码用于按指定类别获取产品,但我想修改此代码以获取类别的完整列表.Category是product表中的一列,因此与每个产品相关的列中会有重复的值。我希望在表中列出用于对产品进行分类的类别列表。如果是这样,下面的答案应该有效,但请注意,返回结果是一个
IEnumerable
,您可以迭代打印
不同类别的完整列表
.public IEnumerable GetAllCategory(){return repository.GetAllProducts().Select(p=>p.Category.distinct();}--我尝试了这个方法,但仍然得到生成错误。您必须将
public IEnumerable
更改为
public IEnumerable
这似乎是处理Ienumerables/list时更好的方法。这对我有效:var details=allDetails.Where(x=>x.EmpId==100)。GroupBy(x=>x.SaleDate)。选择(y=>y.First()).Distinct();很好的解决方案。
public DbSet<SCountry> country { get; set; }
    public List<SCountry> DoDistinct()
    {
        var query = (from m in country group m by new { m.CountryID, m.CountryName, m.isactive } into mygroup select mygroup.FirstOrDefault()).Distinct();
        var Countries = query.ToList().Select(m => new SCountry { CountryID = m.CountryID, CountryName = m.CountryName, isactive = m.isactive }).ToList();
        return Countries;
    }
public class Pair 
{ 
    public int id {get;set;}
    public string Arb {get;set;}
}

void Main()
{

    var theList = new List<Pair>();
    var randomiser = new Random();
    for (int count = 1; count < 10000; count++)
    {
        theList.Add(new Pair 
        {
            id = randomiser.Next(1, 50),
            Arb = "not used"
        });
    }

    var timer = new Stopwatch();
    timer.Start();
    var distinct = theList.GroupBy(c => c.id).Select(p => p.First().id);
    timer.Stop();
    Debug.WriteLine(timer.Elapsed);

    timer.Start();
    var otherDistinct = theList.Select(p => p.id).Distinct();
    timer.Stop();
    Debug.WriteLine(timer.Elapsed);
}