13

我想使用 distinct 从 LIST 中删除重复的行。

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

id  idIndice    idName         idTipo   tamanho     caminho
12  11          Processo       3        10          C:\Program Files\Empenho\Senha.txt
13  13          Endereço       1        250         C:\Program Files\Empenho\Senha.txt
14  12          Número         2        5           C:\Program Files\Empenho\Senha.txt
15  9           Cep            5        8           C:\Program Files\Empenho\Senha.txt 
16  10          Dt. de Nasc.   4        0           C:\Program Files\Empenho\Senha.txt
12  11          Processo       3        10          C:\Program Files\Empenho\Senha.txt
14  12          Número         2        5           C:\Program Files\Empenho\Senha.txt

这是我要归档的sql (这可以完成工作)

select DISTINCT u.id, u.idIndice, t.idName, t.idTipo, t.tamanho, l.caminho
from  tgpwebged.dbo.sistema_Indexacao as u
join  tgpwebged.dbo.sistema_Indexes as t on u.idIndice = t.id
join  tgpwebged.dbo.sistema_Documentos as l on u.idDocumento = l.id
join  tgpwebged.dbo.sistema_DocType_Index as v on t.id = v.indexId
where u.idDocumento = 10 

这是我正在尝试适应的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();

但是我仍然得到相同的 7 行,就好像根本没有 DISTINCT 一样。

为什么?

4

6 回答 6

6

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

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
    { ...  }).Distinct().ToList();
于 2012-12-19T13:38:22.493 回答
5
var docIndexModel = docIndexModelDup
    .GroupBy(x => x.Id)
    .Select(g => g.First());
于 2012-12-19T13:41:30.990 回答
1

尝试:

var distinctRowsById = docObj.Select(i => i.Id)
    .Distinct()
    .Select(i => docObj.First(o => o.Id == i)
于 2012-12-19T13:36:02.897 回答
0

每当使用 sql 时,请在 .ToList() 之前使用 .Distinct()。这可能会解决问题。例如:

var ans = (from x in xyx
             where ...
             select new ...
             {
                a = ...,
                b = ...
             }).Distinct().ToList();
于 2012-12-19T18:11:05.063 回答
0

在您的 getDocIndex 对象上,您需要实现接口 IEquatable。这将告诉 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;
    } 
}

实现这个接口后,可以继续调用 distinct ,它会正常工作。

于 2012-12-19T13:46:28.303 回答
0

Distinct 仅适用于 Lambda ..like this

var data =_CustomerCompanyRepository.GetAll().Where(p => (p.PrimaryUser == userid || p.SecondayUser == userid)).Distinct();

如果我们想删除 linq 查询中的重复项 ..use 组

 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();
于 2017-06-20T09:26:41.430 回答