Home > Net >  Complicate linq query which filter out duplicate value
Complicate linq query which filter out duplicate value

Time:07-14

I have a json as data as below :

  [
        {
            "id": 1,
            "shopname": "seven up",
            "shopkeeper": "John",
            "salesbooks": [
                {
                    "bookid": 11,
                    "bookname": "Tom-story",
                    "catagories" : 1,
                    "soldout": false
                },
                {
                    "bookid": 12,
                    "bookname": "Iron-Man",
                    "catagories" : 2,
                    "soldout": true
                }
            ]
        },
        {
            "id": 2,
            "shopname": "Richmond Shop",
            "shopkeeper": "Jame",
            "salesbooks": [
                {
                    "bookid": 11,
                    "bookname": "Tom-story",
                    "catagories" : 2,
                    "soldout": false
                },
                {
                    "bookid": 13,
                    "bookname": "PeterPan",
                    "catagories" : 2,
                    "soldout": true
                }
            ]
        },
        {
            "id": 3,
            "shopname": "Worchester Shop",
            "shopkeeper": "Jame",
            "salesbooks": [
                {
                    "bookid": 15,
                    "bookname": "Jurias Park",
                    "catagories" : 1,
                    "soldout": false
                },
                {
                    "bookid": 16,
                    "bookname": "Champion",
                    "catagories" : 1,
                    "soldout": true
                }
            ]
        }
    ]

My expected result should be:

[
            {
                "id": 1,
                "shopname": "seven up",
                "shopkeeper": "John",
                "salesbooks": [
                    {
                        "bookid": 11,
                        "bookname": "Tom-story",
                        "catagories" : 1,
                        "soldout": false
                    },
                    {
                        "bookid": 12,
                        "bookname": "Iron-Man",
                        "catagories" : 2,
                        "soldout": true
                    }
                ]
            }
]

Normally, if require to get shop which have book on sales , I can do it

var bookshop = bookshops.where(w=>w.salesbooks.any(a=>a.soldout==false));

But this time, if I also want to filter out if count(salesbooks.catagories) > 1? That mean I want to filter out id:2 bookshop , as both of salesbook.catagories are same. How to write the linq in c#?

p.s. I try

var bookshop = bookshops.where(w=>w.salesbooks.any(a=>a.soldout==false) && w.salebooks.GroupBy(g=>g.catagories).count==1);

the result is still show Richmond shop Thank you


CodePudding user response:

If you just want to filter out bookshops with categories more than 1. In C# You can just group by categories and have a check for >1 rather than ==1 i.e.

C# code var bookshop = bookshops.Where(w => w.salesbooks.Any(a => a.soldout == false) && w.salesbooks.GroupBy(g => g.catagories).Count() > 1;

CodePudding user response:

you can use another clause like

            var bookshop = bookshops.Where(w => w.salesbooks.Any(a => a.soldout == false) && !w.salesbooks.Any(a => a.catagories < 2) );

Hope this helps

  • Related