如何过滤产品 - 产品的区别 [英] how to filter products - Distinct of products

查看:75
本文介绍了如何过滤产品 - 产品的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 我用关键字搜索产品,我得到   结果与重复。我需要过滤不同的值。

 I search a product with key word , i get  the result with duplicates. i need to filter the distinct values.

  list声明如下

 list is declared as follows

  列表与LT;产品与GT; products = new List< Product>();

   List<Product> products = new List<Product>();

我试过这个

products.Distinct()。ToList();

products.Distinct().ToList();

它没有用。

我只需要显示不同的值。我不知道如何过滤它。任何人都可以帮忙吗?

i need to show only distinct values. i didnt know how to filter that. can any one help?

问候,

Kumudha.D

Kumudha.D

推荐答案

您使用的LINQ查询不会返回不同的产品。

The LINQ query you used doesn't return distinct products.

以下链接中给出的更多详细信息如何比较两个对象。还有下面给出的示例代码进行比较。通过这种方式,您只能将产品与少数字段进行比较。例如,如果两个产品相同但创建的日期不同,则可以忽略那些字段而忽略这些字段的

More details given in the following link how to compare two objects. Also sample code to compare given below. In this way you can compare your products with few fields only. For example if two products are equal but created date is different, you can ignore those fields by not cmparing those values.

http://blogs.msdn.com /b/csharpfaq/archive/2009/03/25/how-to-use-linq-methods-to-compare-objects-of-custom-types.aspx

//产品类

 公共类产品:IEquatable<产品>

 public class Product : IEquatable<Product>

    {

    {

        public string ProductId {get;组; }
        public string ProductDescription {get;组; }
        public string ProductColor {get;组; }

        public string ProductId { get; set; }
        public string ProductDescription { get; set; }
        public string ProductColor { get; set; }

       公共无效  Create(string productID,string ProdDesc,string prodColor)

        {

            this.ProductId = productID;

            this.ProductDescription = ProdDesc;

            this.ProductColor = prodColor;

        public void  Create(string productID,string ProdDesc,string prodColor)
        {
            this.ProductId = productID;
            this.ProductDescription = ProdDesc;
            this.ProductColor = prodColor;

        }

        }

        public bool Equals(产品其他)

        {

        public bool Equals(Product other)
        {



            //检查比较对象是否为空。

            if(Object.ReferenceEquals(other,null))返回false;


            // Check whether the compared object is null.
            if (Object.ReferenceEquals(other, null)) return false;



          ;&NBSP;&NBSP; //检查比较对象是否引用相同的数据。

            if(Object.ReferenceEquals(this,other))返回true;


            // Check whether the compared object references the same data.
            if (Object.ReferenceEquals(this, other)) return true;



          ;&NBSP;&NBSP; //检查对象是否’属性相同。

           返回ProductId.Equals(other.ProductId)及;&安培;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP; ProductDescription.Equals(other.ProductDescription)&&

                &NBSP;&NBSP;&NBSP;&NBSP; ProductColor.Equals(other.ProductColor);

        }


            // Check whether the objects’ properties are equal.
            return ProductId.Equals(other.ProductId) &&
                   ProductDescription.Equals(other.ProductDescription)&&
                    ProductColor.Equals(other.ProductColor);
        }



       


       



        public override int GetHashCode()

        {


        public override int GetHashCode()
        {



            //获取ProductId字段的哈希码,如果它不为空或空。

            int hashProductId = ProductId == string.Empty? 0:ProductId.GetHashCode();


            // Get the hash code for the ProductId field if it is not null or empty.
            int hashProductId = ProductId == string.Empty ? 0 : ProductId.GetHashCode();



            //获取ProductDescription字段的哈希码。

            INT hashProductDescription = ProductDescription.GetHashCode();


            // Get the hash code for the ProductDescription field.
            int hashProductDescription = ProductDescription.GetHashCode();

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //获取ProductColor字段的哈希码。

            INT hashProductColor = ProductColor.GetHashCode();

            // Get the hash code for the ProductColor field.
            int hashProductColor = ProductColor.GetHashCode();

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //计算对象的哈希码。

            return hashProductId ^ hashProductDescription ^ hashProductColor;

        }

            // Calculate the hash code for the object.
            return hashProductId ^ hashProductDescription ^ hashProductColor;
        }



    }


    }

//产品比较类

  公众&NBSP; class ProductComparer:IEqualityComparer< Product>

    {

        public bool Equals(产品x,产品y)

        {

            if(Object.ReferenceEquals(x,y))返回true;

   public  class ProductComparer : IEqualityComparer<Product>
    {
        public bool Equals(Product x, Product y)
        {
            if (Object.ReferenceEquals(x, y)) return true;



          ;&NBSP;&NBSP;如果(Object.ReferenceEquals(X,NULL)||

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; Object.ReferenceEquals(y,null))

               返回false;


            if (Object.ReferenceEquals(x, null) ||
                Object.ReferenceEquals(y, null))
                return false;



            return x.ProductId == y.ProductId&& x.ProductDescription == y.ProductDescription&& x.ProductColor == y.ProductColor;

        }


            return x.ProductId == y.ProductId && x.ProductDescription == y.ProductDescription && x.ProductColor == y.ProductColor;
        }



        public int GetHashCode(Product product)

        {

            if(Object.ReferenceEquals(product,null))返回0;


        public int GetHashCode(Product product)
        {
            if (Object.ReferenceEquals(product, null)) return 0;



          ;&NBSP;&NBSP; int hashProductId = product.ProductId == null

                ? 0:product.ProductId.GetHashCode();


            int hashProductId = product.ProductId == null
                ? 0 : product.ProductId.GetHashCode();



&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; INT hashProductColor = product.ProductColor.GetHashCode();


            int hashProductColor = product.ProductColor.GetHashCode();

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; INT hashProductDescription = product.ProductDescription.GetHashCode();

            int hashProductDescription = product.ProductDescription.GetHashCode();

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; return hashProductColor ^ hashProductDescription ^ hashProductId;

        }
    }

            return hashProductColor ^ hashProductDescription ^ hashProductId;
        }
    }



//在按钮点击或控制台应用中测试产品比较



           列表与LT;产品与GT; prod = new List< Product>();

           产品p1 =新产品();

            p1.Create(QUOT; ABC"," ABC1"," ABC2");

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; prod.Add(p1);


            List<Product> prod = new List<Product>();
            Product p1 = new Product();
            p1.Create("ABC", "ABC1", "ABC2");
            prod.Add(p1);

           产品p2 =新产品();

            p2.Create(" ABCD"," ABC2"," ABC3");

            prod.Add(p2);

            Product p2 = new Product();
            p2.Create("ABCD", "ABC2", "ABC3");
            prod.Add(p2);

           产品p3 =新产品();

            p3.Create("ABCDE","ABC3","ABC4");

            prod.Add(p3);

            Product p3 = new Product();
            p3.Create("ABCDE", "ABC3", "ABC4");
            prod.Add(p3);



           产品p4 =新产品();

            p4.Create("ABCDEF","ABC4","ABC5");

            prod.Add(p4);


            Product p4 = new Product();
            p4.Create("ABCDEF", "ABC4", "ABC5");
            prod.Add(p4);



           产品p5 =新产品();

            p5.Create("ABC","ABC1","ABC2");

       

            prod.Add(p5);

           的IEqualityComparer<产品与GT; pe;

            IEnumerable的<产品与GT; ppp = prod.Distinct(new ProductComparer());


            Product p5 = new Product();
            p5.Create("ABC", "ABC1", "ABC2");
       
            prod.Add(p5);
            IEqualityComparer<Product> pe;
            IEnumerable<Product> ppp = prod.Distinct(new ProductComparer());


这篇关于如何过滤产品 - 产品的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆