【问题标题】:How to fetch records without Duplicates using Linq如何使用 Linq 获取没有重复的记录
【发布时间】:2021-02-27 03:19:05
【问题描述】:

嗨,有人可以帮忙吗,我有一个包含我的促销代码的列表,在列表中我想只返回出现一次的促销代码,即没有重复,请参阅下面来自 JSON 的数据,我想返回促销代码 A123 和 B500 并将它们存储在另一个列表中。

[
   {
    "PromCode": "A123",
    "Priority": 1,
    "offer": "Win a Free Cap",
    "StartDte": "2020-08-11T00:16:23.184Z",
    "endDte": "2020-09-10T17:16:23.184Z",
  }, 
  {
    "PromCode": "A100",
    "Priority": 1,
    "offer": "Win a perfume",
    "StartDte": "2020-08-11T00:16:23.184Z",
    "endDte": "2020-09-10T17:16:23.184Z",
  },
{
    "PromCode": "A100",
    "Priority": 2,
    "offer": "Win a Phone pouch",
    "StartDte": "2020-09-11T00:16:23.184Z",
    "endDte": "2020-10-10T17:16:23.184Z",
  },
 {
    "PromCode": "B500",
    "Priority": 1,
    "offer": "Win a free router",
    "StartDte": "2020-08-11T00:16:23.184Z",
    "endDte": "2020-09-10T17:16:23.184Z",
  },

]

我有一个包含所有这些促销代码的列表,如下所示

var existingProms = await _Repo.GetAllPromCodes(promCodeList);

我试图得到像这样在列表中出现一次的那些

var appearOnce = existingProms.Count(x => existingBnplIndicators.Contains(x.PromCode)).ToList()<2;
var appearOnce = existingProms.where(x=> x.PromCode.Count()).ToList()<2;

但这没有用,返回了 0 个结果,有人可以告诉我如何让我的两个 Proms A123,B500 进入我的出现一次。谢谢

【问题讨论】:

    标签: c# json linq .net-core


    【解决方案1】:

    您可以使用GroupBy 获取按促销代码分组的所有结果。然后,根据每个组必须仅在Count() == 1 时显示的项目数过滤结果。

    可能是这样的,

    public class Class1
    {
        public string PromCode { get; set; }
        public int Priority { get; set; }
        public string offer { get; set; }
        public DateTime StartDte { get; set; }
        public DateTime endDte { get; set; }
    }
    
    var obj = JsonConvert.DeserializeObject<List<Class1>>(json);
    var singlesOnly = obj.GroupBy(x => x.PromCode).Where(y => y.Count() == 1);
    

    【讨论】:

    • @Jawas 谢谢你的工作,只需要添加 var singlesOnly = obj.GroupBy(x => x.PromCode).Where(y => y.Count() == 1)。 Select(x=>x.Key.ToString()).ToList();让它返回 singlessOnly 作为字符串列表
    【解决方案2】:

    您应该仅通过 promcode 隐式比较所需的对象。看看 Equals 和 GetHashCode() 是如何工作的。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace test
    {
        public class TestObj : IEquatable<TestObj>
        {
            public string Promocode { get; set; }
            public int Priority { get; set; }
            public string offer { get; set; }
            public DateTime StartDate { get; set; }
            public DateTime endDate { get; set; }
            public override int GetHashCode() => this.Promocode.GetHashCode();
            public bool Equals(TestObj other) => Promocode.Equals(other.Promocode);
        }
        class Program
        {
            static void Main(string[] args)
            {
                var a = new TestObj()
                {
                    Promocode = "1",
                    Priority = 1,
                    offer = "1",
                    StartDate = new DateTime(1, 2, 3),
                    endDate = new DateTime(1, 2, 3)
                };
    
                var b = new TestObj()
                {
                    Promocode = "1",
                    Priority = 1,
                    offer = "1",
                    StartDate = new DateTime(1, 2, 3),
                    endDate = new DateTime(1, 2, 3)
                };
                var c = new TestObj()
                {
                    Promocode = "2",
                    Priority = 1,
                    offer = "1",
                    StartDate = new DateTime(1, 2, 3),
                    endDate = new DateTime(1, 2, 3)
                };
    
                var list = new List<TestObj>()
                {
                    a,
                    b,
                    c
                };
    
                var uniqueOnly = list.Distinct();
    
                foreach (var item in uniqueOnly)
                {
                    Console.WriteLine(item.Promocode);
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      首先定义类项-

              public string PromCode { get; set; }
              public int Priority { get; set; }
              public string offer { get; set; }
              public DateTime StartDte { get; set; }
              public DateTime endDte { get; set; }
      

      然后取出列表中的多个项目并使用 distinct() 函数删除重复值-

      static void Main(string[] args)
          {
              List<TestClass> list = new List<TestClass>()
              {
                  new TestClass(){PromCode="A123",Priority=1,offer="Win a Free 
                  Cap",StartDte=new DateTime(2020-08-11),endDte=new DateTime(2020-09-10)},
      
                  new TestClass(){PromCode="A100",Priority=1,offer="Win a 
                  perfume",StartDte=new DateTime(2020-08-11),endDte=new DateTime(2020-09- 
                  10)},
      
                  new TestClass(){PromCode="A100",Priority=2,offer="Win a Phone 
                  pouch",StartDte=new DateTime(2020-09-11),endDte=new DateTime(2020-10-10)},
      
                  new TestClass(){PromCode="B500",Priority=1,offer="Win a free 
                  router",StartDte=new DateTime(2020-08-11),endDte=new DateTime(2020-09-10)}
              };
      
              var finalList = list.Select(b => b.PromCode).Distinct().ToList();
              foreach(var item in finalList)
              {
                  Console.WriteLine(item + "");
              }
              Console.Read();
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-19
        • 2013-03-22
        • 2015-03-14
        • 2014-03-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多