Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不与LINQ一起工作_C#_Linq - Fatal编程技术网

C# 不与LINQ一起工作

C# 不与LINQ一起工作,c#,linq,C#,Linq,我想使用distinct从列表中删除重复的行 这是结果集(如您所见,索引12和14重复) 这是我要归档的sql(这就可以了) 这是我试图适应的LINQ var docObj = from u in context.sistema_Indexacao join t in context.sistema_Indexes on u.idIndice equals t.id join l in context.sistema_Documentos on u.idDocumento equals l.id

我想使用distinct从列表中删除重复的行

这是结果集(如您所见,索引12和14重复)

这是我要归档的sql(这就可以了)

这是我试图适应的LINQ

var docObj = from u in context.sistema_Indexacao
join t in context.sistema_Indexes on u.idIndice equals t.id
join l in context.sistema_Documentos on u.idDocumento equals l.id
join v in context.sistema_DocType_Index on t.id equals v.indexId
join m in context.sistema_DocType on v.docTypeId equals m.id
where u.idDocumento == id
select new Gedi.Models.OperacoesModel.getDocIndex
{ ...  };
这就是我正在尝试的:

List<Gedi.Models.OperacoesModel.getDocIndex> docIndexModelDup = docObj.ToList();
List<Gedi.Models.OperacoesModel.getDocIndex> docIndexModel =
docIndexModelDup.Distinct().ToList();
List docIndexModelDup=docObj.ToList();
列表docIndexModel=
docIndexModelDup.Distinct().ToList();
但我仍然得到相同的7行,好像根本没有明显的差异

为什么?

试试:

var distinctRowsById = docObj.Select(i => i.Id)
    .Distinct()
    .Select(i => docObj.First(o => o.Id == i)

如果希望在sql中执行Distinct,请在ToList()之前调用Distinct()


在getDocIndex对象上,需要实现接口 这将告诉Distinct方法在进行比较时对象是否彼此相等。这是与Distinct进行比较的正确方法,而且更干净

public class getDocIndex: IEquatable<getDocIndex> 
{
    ....
    public bool Equals(getDocIndex otherModel)
    {
        if (otherModel == null) 
            return false;
        return this.idName == otherModel.idName && this.idTipo == otherModel.idTipo
        && this.tamanho == otherModel.tamanho && this.caminho == otherModel.caminho;
    } 
}
公共类getDocIndex:IEquatable
{
....
公共布尔等于(getDocIndex其他模型)
{
if(otherModel==null)
返回false;
返回this.idName==otherModel.idName&&this.idTipo==otherModel.idTipo
&&this.tamanho==otherModel.tamanho&&this.caminho==otherModel.caminho;
} 
}

实现此接口后,可以继续调用distinct,它将正常工作。

无论何时使用sql,请在.ToList()之前使用.distinct()。 这可能会解决问题。 例如:

var ans = (from x in xyx
             where ...
             select new ...
             {
                a = ...,
                b = ...
             }).Distinct().ToList();

Distinct仅适用于Lambda..像这样

var data =_CustomerCompanyRepository.GetAll().Where(p => (p.PrimaryUser == userid || p.SecondayUser == userid)).Distinct();
如果要删除linq查询中的重复项..请使用组

 var Account = (from c in depa join u in UserManager.Users on c.Id equals u.DepartmentId
                               group c by c into g
                               select new datadto{ Id = g.Key.Id, Name = g.Key.DepatmentName }).ToArray();

getDocIndex
是否实现了
IEquatable
?@Massimiliano Peluso工作得很好。谢谢,我还没有试过,但看着你的代码,我觉得它不会删除重复项,因为每一行当然都有一个Id,它与同一集中的不同Id列表相匹配。你说得对,请现在查看。我以前有过这个问题,我看到解决这个问题的唯一方法就是这种方法。但是,性能比所有其他建议的答案都差,因为它在
docObj
序列上迭代n+1次,其中n是不同id:s的数量。就这么说吧。Feliz Navidad,Nicolás。但这不会影响生成的SQL,这似乎是本文的意图。我只是回答了他的问题。“我想使用distinct从列表中删除重复的行。”“但我仍然得到相同的7行,就好像根本没有distinct一样。为什么?”不幸的是,这并不能解决我的问题。我应该重写某个类来完成它吗?仍然保留列表中的7个iten是否从sql语句中选择相同的列?
public class getDocIndex: IEquatable<getDocIndex> 
{
    ....
    public bool Equals(getDocIndex otherModel)
    {
        if (otherModel == null) 
            return false;
        return this.idName == otherModel.idName && this.idTipo == otherModel.idTipo
        && this.tamanho == otherModel.tamanho && this.caminho == otherModel.caminho;
    } 
}
var ans = (from x in xyx
             where ...
             select new ...
             {
                a = ...,
                b = ...
             }).Distinct().ToList();
var data =_CustomerCompanyRepository.GetAll().Where(p => (p.PrimaryUser == userid || p.SecondayUser == userid)).Distinct();
 var Account = (from c in depa join u in UserManager.Users on c.Id equals u.DepartmentId
                               group c by c into g
                               select new datadto{ Id = g.Key.Id, Name = g.Key.DepatmentName }).ToArray();