LINQ+EF如何显示的时候去掉重复项

qq_35913485 2016-11-22 03:34:13

如图所看有两个字段是重复的 我想更具这两个重复的字段删掉重复想 怎么写 后面的是现在的代码 求大神指教
...全文
3604 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
首先得看业务上页面需要展示什么,虽然名称一样,但是其他列有不一样的数据, 如果去重且表格要显示其他列那就只能取其中一条展示, 那就group by相同数据列,然后排序取第一条 如果不需要展示其他列数据,那直接查出来的时候不查那些数据不同的列(所属编码、商品编号),整个结果集distinct一下就好了。
clsssssssssssssss 2019-11-20
  • 打赏
  • 举报
回复
Distinct
clsssssssssssssss 2019-11-07
  • 打赏
  • 举报
回复
distinct
XBodhi. 2019-09-21
  • 打赏
  • 举报
回复
引用 12 楼 Dear200892 的回复:

    /// <summary>
    /// 去除Map集合中重复的方格
    /// </summary>
    public class MapListDistinct : IEqualityComparer<Map>
    {
        public bool Equals(Map x, Map y)
        {
            if (x == null || y == null)
                return false;
            if (x.PointX == y.PointX && x.PointY == y.PointY)
                return true;
            else
                return false;
        }

        public int GetHashCode(Map obj)
        {
            if (obj == null)
                return 0;
            else
                return obj.PointX.GetHashCode() ^ obj.PointY.GetHashCode();
        }
    }
其中 if (x.PointX == y.PointX && x.PointY == y.PointY)和obj.PointX.GetHashCode() ^ obj.PointY.GetHashCode(); 这两段内容是去除重复重点,将其修改成你需要比较的字段 使用方法:

            //去除重复的方格
            mapsList = mapsList.Distinct(new MapListDistinct()).ToList();
可行,不过,我不怎么用 EF ,喜欢用自己写的 ORM
快乐起航2020 2019-09-20
  • 打赏
  • 举报
回复
结尾distinct 不行么?
Dear200892 2019-08-27
  • 打赏
  • 举报
回复

    /// <summary>
    /// 去除Map集合中重复的方格
    /// </summary>
    public class MapListDistinct : IEqualityComparer<Map>
    {
        public bool Equals(Map x, Map y)
        {
            if (x == null || y == null)
                return false;
            if (x.PointX == y.PointX && x.PointY == y.PointY)
                return true;
            else
                return false;
        }

        public int GetHashCode(Map obj)
        {
            if (obj == null)
                return 0;
            else
                return obj.PointX.GetHashCode() ^ obj.PointY.GetHashCode();
        }
    }
其中 if (x.PointX == y.PointX && x.PointY == y.PointY)和obj.PointX.GetHashCode() ^ obj.PointY.GetHashCode(); 这两段内容是去除重复重点,将其修改成你需要比较的字段 使用方法:

            //去除重复的方格
            mapsList = mapsList.Distinct(new MapListDistinct()).ToList();
  • 打赏
  • 举报
回复
query.groupby(p=>new{ p.GoodsName,p.UserName}).Select(p=>g.First());
叫我 Teacher 周 2019-08-16
  • 打赏
  • 举报
回复
Distinct
¿?¿? 2019-08-16
  • 打赏
  • 举报
回复
可以使用Distinct去除重复,也可以使用groupby进行去重
tianlang_2008 2018-06-07
  • 打赏
  • 举报
回复
query.groupby(p=>p.GoodsName).Where(p=>p.Count()>1).Select(p=>p.Max(ID)).ToList(); 然后你再根据这个Id的List去做删除处理就行了
坤灵小舍 2017-12-05
  • 打赏
  • 举报
回复
var SDLDelivered1 = db.NikeDTCDailyReport.Where(p => p.CN_Launch_Date >= date1 && p.CN_Launch_Date <= date2 && p.CN_Planning_Qty != 0 && p.LW_Specs_Review == null && p.Sample_Batch == null && p.Review_Request_to_Category == null && p.Review_back_from_Category == null ).Where(p => p.CN_CN_Copy_Status__Y_N_ == "Y").Select(p=>p.Style).ToList(); var SDLDelivered2 = SDLDelivered1.Distinct().Count(); 转成集合使用Distinct来去除重复
qq_15148111 2017-10-14
  • 打赏
  • 举报
回复
使用LINQ的分组写法瑟
dengchenlu 2017-08-02
  • 打赏
  • 举报
回复
GroupBy(a=>a.字段).Select(a=> a.OrderBy(x=>x.字段).FirstOrDefault()); 这样可以根据某个字段去除重复的,你看看怎么改成你想要的
Kevin_Zhuang 2017-08-01
  • 打赏
  • 举报
回复
1L 都告诉你了,你return 之前做下处理不就好了
Kevin_Zhuang 2017-08-01
  • 打赏
  • 举报
回复
1L 都告诉你了,你return 之间做下处理不就好了
  • 打赏
  • 举报
回复
初始化赋值的时候 去掉
q107770540 2016-11-23
  • 打赏
  • 举报
回复
use Enumerable.Distinct<TSource> Method (IEnumerable<TSource>, IEqualityComparer<TSource>) http://blog.csdn.net/q107770540/article/details/5784646?locationNum=1&fps=1 https://msdn.microsoft.com/en-us/library/bb338049.aspx

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧