sql one to many output as displayed in image

Viewed 38

cookie-one to many

sampledata

I have table cookie details consisting of cookiename, host, defaultcategory,description, cookieid, domaincookieid, domainname, displayGroupName. A cookie is having one to many relation with domain. I need output where if changecategory is different from defaultcategory then I need count of overridden category for each cookie aand total domain count for that particular cookie. Suppose if default category is "Analytics and Performance" and it getting overridden in displayGroupName column to "strictly necessary".So in image 1 i need count in domaincategory override to 1 and domains as 2.

 public class Content
    { public string cookieName { get; set; }
        public string lifespan { get; set; }
        public string host { get; set; }
        public string defaultCategory { get; set; }
        public string cookieSource { get; set; }
        public string defaultDescription { get; set; }
        public string defaultThirdPartyDescription { get; set; }
        public string expiry { get; set; }
        public bool thirdParty { get; set; }
        public List<DomainCookieInfoDtoList> domainCookieInfoDtoList { get; set; }
    }

  public class DomainCookieInfoDtoList
    {
        public string cookieId { get; set; }
        public string domainCookieId { get; set; }
        public string domainName { get; set; }
        public string displayGroupName { get; set; }
        public string cookieCategoryID { get; set; }
        [JsonProperty(PropertyName = "thirdParty")]
        public bool domainthirdParty { get; set; }
        public string description { get; set; }
    }

 public class Root
    {
        public List<Content> content { get; set; }
    }

Display cookie with the count of domains and category count which gets overridden?

1 Answers

if your data objects are filled from the db then to output the count you need (not the most efficient but easy to debug and understand):

var result = root.content.Select(i => new 
{ 
    name = i.cookieName,
    defCat = i.defaultCategory,
    overCats = c.domainCookieInfoDtoList
        .Select(oc => oc.displayGroupName).ToList(),
})
.ToList();

result.ForEach(i =>
    Console.WriteLine("Cookie: {0} Domain Category Override {1} Domains {2}",
        i.name,
        i.overCats.Count(oc => dc != i.defCat),
        i.overCats.Count(dc => dc == i.defCat)
));
Related