【问题标题】:Get Distinct rows according to one column and order by date根据一列获取不同的行并按日期排序
【发布时间】:2013-01-28 23:53:36
【问题描述】:

我有以下场景可以在数据库级别或 Linq to EF 级别解决: 这是我在数据库中的视图:

id  title   date           weight
==================================
1   t1     2013-01-18       1.5
1   t1     2013-01-17       1.4
1   t1     2013-01-15       1.31
1   t1     2013-01-12       1.22
2   t2     2013-01-19       2.3
2   t2     2013-01-16       2.1
2   t2     2013-01-07       1.81
2   t2     2013-01-19       1.62

因此,我需要的是每个项目(t1 和 t2)中的一条记录,这是按日期计算的最新记录。

所以输出会是这样的:

id  title   date           weight
==================================
1   t1     2013-01-18       1.5
2   t2     2013-01-19       2.3

正如我上面所说,欢迎使用 (Distinct) 在数据库级别或 linq 级别上的答案。

我的 c# linq 的一部分:

mylist = (from a in db.myview
join art in db.viewTags on a.id equals art.ArticleID
where (art.TagID == tag.ID)
select a).Distinct().Take(10).ToList();

我需要根据 a.id(视图的 id 字段)与 myview 不同的记录

谢谢

【问题讨论】:

    标签: c# sql sql-server entity-framework


    【解决方案1】:

    EDIT - 根据您希望通过 id 区分的更新

    全文:DistinctBy in Linq (Find Distinct object by Property)

    以下是MoreLINQ 库的一部分。

    使用DistinctBy函数

    public static IEnumerable<TSource> DistinctBy<TSource, TKey>
        (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
    {
        HashSet<TKey> seenKeys = new HashSet<TKey>();
        foreach (TSource element in source)
        {
            if (seenKeys.Add(keySelector(element)))
            {
                yield return element;
            }
        }
    }
    

    因此,要仅使用 Id 属性查找不同的值,您可以使用:

    mylist = (from a in db.myview
     join art in db.viewTags on a.id equals art.ArticleID
     where (art.TagID == tag.ID)
     select a).DistinctBy(a=>a.Id).Take(10).ToList();
    

    select * from table 
    inner join
    (select max(date) as date,id from table group by id) d 
    on d.id = table.id and d.date= table.date
    

    【讨论】:

    • 我的 linq select 语句中已经有一个 join,我可以做这个 join 吗?还有一件事我需要从视图中返回大约 10 行的所有字段
    • @AliIssa - 如果您可以粘贴一些代码,我可以帮助您...因为我不能直接对此说些什么..
    • 第一部分和第二部分代码有关系吗?我也不能这样做“Distinct(a=>a.Id)”我收到一个错误:无法将 lambda 表达式转换为类型 'System.Collections.Generic.IEqualityComparer' 因为它不是委托类型
    • @AliIssa - 嘿,你需要使用 DistinctBy 方法,并且需要在你的项目中包含 morelinq.dll ......这对你有用......
    • 我也没有 distinctby,我使用的是实体框架 4.1
    【解决方案2】:

    即使同一日期有 2 个重量,以下内容也会给您一行:

    declare @t table (
        id int,
        title varchar(50),
        date datetime,
        weight decimal(19,4)
    )
    
    insert into @t (id, title, date, weight) values
       (1, 't1', '20130118', 1.5),
       (1, 't1', '20130118', 1.6),
       (2, 't2', '20130116', 1.4),
       (2, 't2', '20130115', 1.2)
    
    select
        *
    from
        (
            select ROW_NUMBER() over (partition by id order by date desc) rn, * 
            from @t
        ) v
    where rn = 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      相关资源
      最近更新 更多