精华内容
下载资源
问答
  • linq去重复

    千次阅读 2015-05-04 13:05:27
    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) {
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        
        class Program
        {
            static void Main(string[] args)
            {
                List<Class1> list = new List<Class1>()
                {
                    new Class1{ Name="a1", Url="http://www.baidu.com"},
                    new Class1{ Name="a2", Url="http://www.baidu.com"},
                    new Class1{ Name="a3", Url="http://www.google.com"}
                };
    
               // var result = list.Distinct(new Class1Compare()).Take(5).ToList();
                var result = list.GroupBy(x => x.Url).Select(x => x.First()).Take(5).ToList();
                result.ForEach(x => Console.WriteLine("A:{0},B:{1}",x.Name,x.Url));
                
                Console.ReadKey();
            }
        }
    
        public class Class1
        {
            public string Name { get; set; }
            public string Url { get; set; }
        }
    
        public class Class1Compare : IEqualityComparer<Class1>
        {
            public bool Equals(Class1 x, Class1 y)
            {
                return string.Equals(x.Url, y.Url);
            }
    
            public int GetHashCode(Class1 obj)
            {
                if (Object.ReferenceEquals(obj, null)) return 0;
                return obj.Url.GetHashCode();
    
            }
    
        }
    }
    

    展开全文
  • Linq去除重复的数据

    千次阅读 2013-11-06 16:29:17
    Linq去除重复的数据
    //①一般的List集合去重
    List<string>list=new List<string>();
    list.add("AAA");
    list.add("AAA");
    list.add("BBB");
    list.add("AAA");
    
    list=list.Distinct().ToList();
    
    //②实体类List集合去重  
    public class TravelTrafficInfo
        {
            /// <summary>
            /// 行程号
            /// </summary>
            public int JouneryNum { get; set; }
            /// <summary>
            /// 机票名称
            /// </summary>
            public string TrafficName { get; set; }
        }
        public class TravelTrafficInfoComparer : IEqualityComparer<TravelTrafficInfo>
        {
        	 #region IEqualityComparer<User> 成员
            public bool Equals(TravelTrafficInfo x, TravelTrafficInfo y)
            {
            	 if (x.JouneryNum == y.JouneryNum && x.TrafficName == y.TrafficName)
            	 	  return true;
                else
                    return false;
            }
    
            public int GetHashCode(TravelTrafficInfo obj)
            {
                return 0;
            }
            #endregion
        }
        //去除重复的航班
         List<TravelTrafficInfo> List = list.Distinct(new TravelTrafficInfoComparer()).ToList();
        
    //③例如:Test是个实体类,有属性Name、ID
    List<Test>list=得到一个list集合;
    list=list.Where(t=>t.Name("张三")).ToList();//得到名字中包含张三的

    展开全文
  • 1, 两个List泛型用Linq去重复数据 Code: List<string> lstA = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E004....

    1, 两个List泛型用Linq去重复数据

    Code:

                List<string> lstA = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568" };
                List<string> lstB = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568", "E00401501B652569" };
    
    
                List<string> newList = lstA.FindAll(x => !lstB.Contains(x));
                foreach (var item in newList)
                {
                    Console.WriteLine(item);
                }
                Console.ReadKey();

     

    控制台运行结果

    图1

     

    图1 为什么会出现这个情况,什么都没有输出。下面修改下程序,请看2

     

    2,先看 Code:

                List<string> lstA = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568" };
                List<string> lstB = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568", "E00401501B652569" };
    
    
                List<string> newList = lstB.FindAll(x => !lstA.Contains(x));
                foreach (var item in newList)
                {
                    Console.WriteLine(item);
                }
                Console.ReadKey();

    再看结果:

    图2

     看下加粗线 的代码 和图2结果,想必你看出所以然了。

    把 A数组 当做 A区间 B数组 当做 B区间, B区间的 范围大于 A区间 ,把A、B两区间的 公共部分除掉,不同部分找出来。

    1 结果 没有数据输出,原因就在此。

     3,在 A数组 在 加个 mac地址 这时候 A、B两区间范围一样大

    code:

                List<string> lstA = new List<string> { "E00401501B652562", "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568" };
                List<string> lstB = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568", "E00401501B652569" };
    
    
                List<string> newList = lstB.FindAll(x => !lstA.Contains(x));
                foreach (var item in newList)
                {
                    Console.WriteLine(item);
                }
                Console.ReadKey();

    再看结果:

    图3

     

    图3和图2的结果一样,不重复的数据 应该是 E00401501B652562,E00401501B652569这两个mac地址才正确。

    这时候代码怎么调整呢?看 4

     

    4,Code

                List<string> lstA = new List<string> { "E00401501B652563", "E00401501B652562", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568" };
                List<string> lstB = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568", "E00401501B652569" };
                List<string> listAll = new List<string>();
                List<string> listResult = new List<string>();
    
                var listUnionAll = lstA.Union(lstB).OrderBy(t => t);//排序合并数据
                
                foreach (var item in listUnionAll)
                {
                    listAll.Add(item);
                }
    
                List<string> newList = listAll.FindAll(x => !lstA.Contains(x));//去重复,组合新的List集合
                List<string> newList2 = listAll.FindAll(x => !lstB.Contains(x));
    
                var Unionlist = newList.Union(newList2).OrderBy(t => t);//排序合并数据
    
                foreach (var item in Unionlist)
                {
                    Console.WriteLine(item);
                }
                Console.ReadKey();

     

    结果

     

    5,找重复数据 把linq不等于号去掉就行了。

                List<string> lstA = new List<string> { "E00401501B652562", "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568" };
                List<string> lstB = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568", "E00401501B652569" };
    
    
                List<string> newList = lstB.FindAll(x => lstA.Contains(x));
                foreach (var item in newList)
                {
                    Console.WriteLine(item);
                }
                Console.ReadKey();

    结果

    mac地址 62和69没有输出。

    转载于:https://www.cnblogs.com/suntanyong88/p/4241790.html

    展开全文
  • linq 去重复问题

    2012-08-23 10:47:02
    return db.product.Where(t => t.p_type == type).Distinct().ToList();这样去除重复数据,没有效果,求教
  • using (hsim_ghEntities1 db = new hsim_ghEntities1()) { ObjectQuery<vhs_pfUserRight> vhs = db.vhs_pfUserRight; var menquery = from v in vhs...

     using (hsim_ghEntities1 db = new hsim_ghEntities1())
                {
                    ObjectQuery<vhs_pfUserRight> vhs = db.vhs_pfUserRight;
                    var menquery = from v in vhs
                                   where v.UserID == username
                                   orderby v.ModuleID ascending
                                   select new { ModuleName = v.ModuleName, ModuleID = v.ModuleID };
                    var dismen = menquery.Distinct();
                    foreach (var men in dismen)
                    {
                        MenuItem menuitem = new MenuItem(men.ModuleName);
                        menu.Items.Add(menuitem);
                        vhs_pfUserRightBll vprbll = new vhs_pfUserRightBll();
                        List<vhs_pfUserRight> vpr = vprbll.Getmenus(username, men.ModuleName);
                        foreach (vhs_pfUserRight vp in vpr)
                        {
                            MenuItem menutm = new MenuItem(vp.ProgName);
                            menutm.NavigateUrl = vp.URL + "?pid=" + vp.ProgID;
                            menutm.Target = "hs_ifContent";
                            menuitem.ChildItems.Add(menutm);
                        }

                    }
                }

    转载于:https://www.cnblogs.com/zhhjuly/archive/2012/10/30/2745927.html

    展开全文
  • 我想把重复的数据去掉,用.Distinct 但是不会写啊。 求解答啊。 var userlist=(from user in dc.Users where user.name.contains(key) select user).Distinct(); 这是查询单项,但是我查询出来很多项啊。
  • 这是执行前和执行后想要的效果 以下是用Sql语句实现的代码: select goodsno, goodsspec,SUM([count]) as count from goods ...下面这个则是用Linq实现的代码: var res = (from l in picModel ...
  • var data = (from u in _stone_storageService.LoadSearchData(roleQuery).AsEnumerable() select new { ... 以S_Number字段去除重复值,其余字段可以有重复值。 求大神指教!!!
  • linq list去除重复

    2015-10-11 10:49:27
    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication5 { class Program { static void Main(string[] args)
  • linq list 去除重复

    千次阅读 2018-01-12 11:26:18
    list=list.GroupBy(t=>t.issuercode).Select(t=>t.First()).ToList();
  • 转载:...只可惜linq默认不支持.Distinct(p => p.ID); 试想如果能写成下面的样子,是不是更简单优雅: var p1 = products.Distinct(p => p.ID);...
  • Linq分页去重复

    2010-12-03 11:15:00
    <br />分页必须使用OrderBy方法进行分页,不能... 如果同时要去重复值可以使用Distinct方法,不过Distinct方法,必须出现在OrderBy方法之前 方法使用顺序:.Distinct().OrderBy().Skip().Take()
  • LINQ扩展实现去重复

    2016-09-12 14:21:00
    /// <summary> /// 自定义去重复 /// </summary> public static IEnumerable<TSource> IDistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSour...
  • Linq去除List的重复

    2013-01-25 10:31:56
    中想去掉多个IssuerCode和LACode重复的 比如:List<DC>里面有 peter,I001,LA001,LAcc jenny,I001,LA001,LAcc macus,I002,LA002,LAdd joan,I002,LA002,LAdd tina,I003,LA003,LAee ...
  • Linq Distinct List 去重复

    2014-04-18 15:23:00
    //调用 return producePlantlst.Distinct(new item_collection_DistinctBy_item1()).ToList(); //方法 public class item_collection_DistinctBy_item1 : IEqualityComparer<FMDS_ProducePlantInfo&...
  • 按user分组,取每组的第一个:varo=fromrinxe.Descendants("customer")grouprbyr.Element("User").Valueintogselectg.OrderBy(r=>r.Element("User").V...
  • s.Where((x, i) => s.FindIndex(z => z.ArticleTitle == x.ArticleTitle) == i).ToList(); 转载于:https://www.cnblogs.com/NotEnough/p/10839942.html
  • Menu,这三个表之间有如下关系: User_Role=>RoleId=>RoleMenu RoleMenu=>MenuId=>Menu 它们之间的业务关系是: 当用户登陆后,通过UserId得到User_Role列表,将用户所包括的角色得出 通过User_Role找到所有对应Menu ...
  • Linq实现DataTable动态去重复

    千次阅读 2015-07-28 09:34:33
    本文介绍一种按字段动态对DataTable去除重复数据/// /// 按照fieldName从sourceTable中选择出不重复的行, /// 并且返回sourceTable中所有的列。 /// /// 源表 /// 字段数组 public static DataTable ...
  • linq 中Distinct去除重复数据

    千次阅读 2013-09-12 10:22:49
    调用方法: IListBrandInfo> list = Brandlist.Where(c => c.AreaID == a.AreaID).Distinct(new Comparint()).ToList();  //去除物业下区域中相同分类(CategoryIDs)  public class Comparint : ...
  • 现在假设我们要 按照 Name来筛选重复呢? 很明显,需要再添加一个类ProductNameComparer. 那能不能使用泛型类呢?? 新建类PropertyComparer 继承IEqualityComparer 内容如下: public class ...
  • List<ThemeComment> commentlist = _themecomment.Entities.Where(t => t.UserID == MemberID).GroupBy(t => t.ThemeID).ToList().Distinct() as List<ThemeComment>;
  • linq to sql 单列去重复

    2016-02-24 13:50:33
    用户表 user id name year 1 小李 15 2 小王 14 3 小黑 13 ...要查询 每个岁数 出来一个代表 用LINQ 来查询 最后的结果是 1 小李 15 2 小王 14 3 小黑 13 这语句怎么写? 新手 。。。
  • Linq 和 list集合 去除重复

    千次阅读 2012-09-05 16:33:03
    方法一: IQueryable listNotice = null;  listNotice = (from news in base.CabinData....此种去除方法弊端:只要对象中有重复的字段都会去除,会比对对象中所有的字段。 方法二: 把linq查询结果转成li

空空如也

空空如也

1 2 3 4 5 ... 9
收藏数 167
精华内容 66
关键字:

linq去重复