linq随机选择和差集
来源:互联网 发布:国外有淘宝吗 编辑:程序博客网 时间:2024/06/07 01:49
群里一妹子问了个问题,集合随机选取10条,然后在剩下的数据里面再随机取,大致可以用下面的思路(用的linq)
int[] tt = { 1, 12, 5, 4, 8, 36, 15, 74, 13, 44, 121, 3, 9 };Console.WriteLine ("============随机取10个===========");//这里直接tolist,由预编译表达式转对象,不然下面的except会得不到想要的差集var q = tt.OrderBy (e => Guid.NewGuid ()).Take (10).ToList (); q.ForEach (x => Console.WriteLine (x));Console.WriteLine ("============取得差集===========");var l = tt.Except (q);l.ToList ().ForEach (x => Console.WriteLine (x));
http://msdn.microsoft.com/zh-cn/library/bb300779(v=vs.110).aspx
double[] numbers1 = { 2.0, 2.1, 2.2, 2.3, 2.4, 2.5 }; double[] numbers2 = { 2.2 }; IEnumerable<double> onlyInFirstSet = numbers1.Except(numbers2); foreach (double number in onlyInFirstSet) Console.WriteLine(number);如果希望比较某种自定义数据类型的对象的序列,则必须在您的类中实现 IEqualityComparer<T> 泛型接口
public class Product : IEquatable<Product>{ public string Name { get; set; } public int Code { get; set; } public bool Equals(Product other) { //Check whether the compared object is null. if (Object.ReferenceEquals(other, null)) return false; //Check whether the compared object references the same data. if (Object.ReferenceEquals(this, other)) return true; //Check whether the products' properties are equal. return Code.Equals(other.Code) && Name.Equals(other.Name); } // If Equals() returns true for a pair of objects // then GetHashCode() must return the same value for these objects. public override int GetHashCode() { //Get hash code for the Name field if it is not null. int hashProductName = Name == null ? 0 : Name.GetHashCode(); //Get hash code for the Code field. int hashProductCode = Code.GetHashCode(); //Calculate the hash code for the product. return hashProductName ^ hashProductCode; }}
Product[] fruits1 = { new Product { Name = "apple", Code = 9 }, new Product { Name = "orange", Code = 4 }, new Product { Name = "lemon", Code = 12 } }; Product[] fruits2 = { new Product { Name = "apple", Code = 9 } }; //Get all the elements from the first array //except for the elements from the second array. IEnumerable<Product> except = fruits1.Except(fruits2); foreach (var product in except) Console.WriteLine(product.Name + " " + product.Code); /* This code produces the following output: orange 4 lemon 12 */
当然,except本身提供一个重载,可以参数化比较器,参考:http://msdn.microsoft.com/zh-cn/library/bb336390(v=vs.110).aspx
Product[] fruits1 = { new Product { Name = "apple", Code = 9 }, new Product { Name = "orange", Code = 4 }, new Product { Name = "lemon", Code = 12 } }; Product[] fruits2 = { new Product { Name = "apple", Code = 9 } }; //Get all the elements from the first array //except for the elements from the second array. IEnumerable<Product> except = fruits1.Except(fruits2, new ProductComparer()); foreach (var product in except) Console.WriteLine(product.Name + " " + product.Code); /* This code produces the following output: orange 4 lemon 12 */
0 0
- linq随机选择和差集
- linq 不同对象集合 交集 差集
- 随机洗牌算法和随机选择算法
- Linq学习__07__差集Except、并集Union 和 Concat(TSource)
- 最大最小值选择 和随机选择
- C# Linq 交集、并集、差集、去重
- C# Linq 交集、并集、差集、去重
- 【转】C# Linq 交集、并集、差集、去重
- c# linq的差集,并集,交集,去重
- Linq 数据操作,两个数组求差、交集、并集
- linq实现随机查询
- 随机选择
- 随机选择
- Linq随机读取数据浅析
- 随机算法之随机选择
- 使用随机森林和mRMR进行特征选择
- C++随机选择的实现 rand函数和rand_r
- LINQ操作数组代码(交集,并集,差集,最值,平均,去重复)
- Java中List和ArrayList的区别
- 使用Haproxy时apache日志的配置方法
- CSS clip元素 rect属性中各个数字的含义
- Apache Cordova App Reloader
- <摘>android手机自带浏览器无法识别apk文件
- linq随机选择和差集
- Java常用类
- HTML5做不到的5件事
- maven scope含义的说明
- 设计模式之遵守的原则
- vbdfbfd
- jQuery垂直Tabs新闻滑块
- 京东确认集团架构一分为四:与腾讯电商业务的整合正在进行时,金融和海外拓展受重视
- 编程规范---千万不要把 bool 设计成函数参数