c# - 明显不与 LINQ 一起工作

标签 c# linq

<分区>

我想使用 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 一样。

为什么?

最佳答案

如果你想在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();

关于c# - 明显不与 LINQ 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13953535/

相关文章:

c# - 阶梯模式实现

c# - 有什么方法可以检测 .NET 中的 RTL 语言吗?

c# - Identity Column 的 Int32 限制

c# - mvc 3 中的 resx 本地化

c# - 将 LINQ orderby 转换为就地列表排序

c# - .NET 5.0 对字符串比较的 `IEnumerable<T>.OrderBy` 行为的重大更改

c# - 如果不支持包含,如何在 LINQ to Entities( Entity Framework )中执行 SQL 样式 'IN' 语句?

c# - LINQ 条件 where 仅当 prop 不为 null 时

.net - 如何在动态 linq 查询中使用 "contains"或 "like"?

c# - 平衡导向图