区别在LINQ c#代码中不起作用 [英] Distinct does not work in a LINQ c# code

查看:62
本文介绍了区别在LINQ c#代码中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在实施此代码时遇到问题:



  public   static 列表< SubCategoryInfo> GetPageByCategorySEOName( string  PageName, string  SubPageName)
{
return 来自 p GetAllPageByCategorySEOName()
< span class =code-sdkkeyword> where (p.CategorySEOName.Equals(PageName,StringComparison.OrdinalIgnoreCase)||
p.SubCategorySEOName.Equals(SubPageName,StringComparison.OrdinalIgnoreCase))
&&(p.CategorySEOName.Equals(PageName,StringComparison.OrdinalIgnoreCase)||
p.SubCategorySEOName.Equals(SubPageName,StringComparison.OrdinalIgnoreCase))
选择 p).Distinct()。ToList();
}





这个想法来自这个SQL查询,它按预期工作:



 选择  distinct  c.CategorySEOName 来自 [dbo]。[SubCategory] ​​Sc 
inner join [dbo]。[类别] C on C.CategoryId = Sc.CategoryId
其中(c.categorySEOName in ' aboutus' Sc.SubcategorySEOName in ' '))
(c.categorySEOName 中的code-keyword>(' aboutus ' Sc.SubcategorySEOName in ' 我们的值'))





sql查询工作正常,但不是linq c#代码。



请大家帮忙告诉我为什么linq c#代码的不同之处不起作用,就我而言它复制数据是为什么我想使用不同的sql查询工作正常。

解决方案

尝试GroupBy而不是选择...例如.. 。



列表<   >  distinctPeople = allPeople 
.GroupBy(p => p.PersonId)
。选择(g => g.First())
.ToList();


使用DISTINCT我想你想要删除重复项。

但你在Linq代码中对象级别进行DISTINCT,即在对象p上。

对象通过其GetHashCode进行比较价值



在sql中你做的很好p.CategorySEOName



你需要的是一个区别采取表达

但是这不存在:)



您可以使用group来代替。



myList.GroupBy(p => p.CategorySEOName).Select(g => g.First())


如何使用LINQ从列表中获取不同的值 - Unboxcode

Hi,

I am getting an issue while implementing this code:

public static List<SubCategoryInfo> GetPageByCategorySEOName(string PageName, string SubPageName)
   {
       return (from p in GetAllPageByCategorySEOName()
               where (p.CategorySEOName.Equals(PageName, StringComparison.OrdinalIgnoreCase) ||
                      p.SubCategorySEOName.Equals(SubPageName, StringComparison.OrdinalIgnoreCase))
                     && (p.CategorySEOName.Equals(PageName, StringComparison.OrdinalIgnoreCase) ||
                     p.SubCategorySEOName.Equals(SubPageName, StringComparison.OrdinalIgnoreCase))
                     select p).Distinct().ToList();
   }



The idea come from this sql query which work as expected:

select distinct  c.CategorySEOName from [dbo].[SubCategory] Sc
     inner join [dbo].[Category] C on C.CategoryId = Sc.CategoryId
     where (c.categorySEOName in('aboutus') or Sc.SubcategorySEOName in('""'))
     and ( c.categorySEOName in('aboutus') or Sc.SubcategorySEOName in('Our-values'))



the sql query work fine with distinct but not for the linq c# code.

Please can anyone help telling me why the distinct of the linq c# code do not work, in my case it duplicates data is why I want to use distinct as for the sql query which work fine.

解决方案

Try GroupBy and than do the select... For example ...

List<Person> distinctPeople = allPeople
  .GroupBy(p => p.PersonId)
  .Select(g => g.First())
  .ToList();


With DISTINCT I suppose that you want to remove duplicates.
But you are doing the DISTINCT on object level in the Linq code, i.e on object p.
objects are compared by their GetHashCode value

In the sql you are doing Distinct on p.CategorySEOName

What you need is a Distinct that take an expression
But that doesn't exist :)

You can possibly use group by instead.

myList.GroupBy(p => p.CategorySEOName).Select(g => g.First())


How to get Distinct values from a LIST using LINQ – Unboxcode


这篇关于区别在LINQ c#代码中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆