日期:2014-05-20  浏览次数:21075 次

linq随机选择和差集

群里一妹子问了个问题,集合随机选取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
        */