How to fetch unique objects using Linq

Hi can someone assist please,i have a list that contain my promotions codes and in the list i would like to return only promotion codes that appear once i.e dont have duplicates,please see below data from JSON,i would like to return Promotion code A123 and B500 and store them in another list.

[
   {
    "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",
  },
 {
    "PromCode": "H300",
    "Priority": 2,
    "offer": "Win a free router",
    "StartDte": "2020-08-11T00:16:23.184Z",
    "endDte": "2020-09-10T17:16:23.184Z",
  },

]

I have a list that contains all this promotion codes as seen below,note:and i have already serialized the JSON
objects successfully

var existingProms = await _Repo.GetAllPromCodes(promCodeList);

i tried to get ones that appear once in the list like this

  var distinctList = existingProms.GroupBy(x => x.PromCode).Where(y => y.Count() == 1)
                   .Select(x => x.Key.ToString()).ToList();

Please note,this above returns distinct proms(B500,A123,H300) but return the PromCode only and not all the other fields in that object.I want to return a list of all my objects where distinct and not and where the priority is =1.meaning the distinct List final result must hold two objects as a result,see below illustrated.

distinctList = {
    "PromCode": "A123",
    "Priority": 1,
    "offer": "Win a Free Cap",
    "StartDte": "2020-08-11T00:16:23.184Z",
    "endDte": "2020-09-10T17:16:23.184Z",
  }, 
 {
    "PromCode": "B500",
    "Priority": 1,
    "offer": "Win a free coffee mug",
    "StartDte": "2020-08-11T00:16:23.184Z",
    "endDte": "2020-09-10T17:16:23.184Z",
  },

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Just an additional modification of your original query gets you there:

        var distinctList = existingProms
            .GroupBy(x => x.PromCode)
            .Where(y => y.Count() == 1 && y.First().Priority == 1)
            .Select(x => x.First())
            .ToList();

Since y.Count() == 1 ensures you get only one item in group you can use First to take it and filter Priority by it in .Where(y => y.Count() == 1 && y.First().Priority == 1) and in the next method call select that object.

Above was the method syntax example, in query syntax this would be:

    var distinctList = (from ep in existingProms
                        group ep by ep.PromCode into g
                        where g.Count() == 1 && g.First().Priority == 1
                        select g.First())
                        .ToList();

Method 2

In your groupby you need to select the First element of the grouping instead of selecting the Key:

var distinctList = existingProms
    .GroupBy(x => x.PromCode)
    .Where(y => y.Count() == 1)
    .Select(x => x.First()) // Changed from .Select(x => x.Key.ToString())
    .ToList();


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x