Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how would i do a groupby in this instance ?[modified]

how would i do a groupby in this instance ?[modified]

Scheduled Pinned Locked Moved C#
databasehelpquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tonyonlinux
    wrote on last edited by
    #1

    How would I do something like this

    var distinctBooks = query
    .GroupBy(x => x.BookID)
    .Select(x => x.FirstOrDefault());

    to this

    List<FillGrid> query = (from b in db.Books
    join ba in db.BookAuthors on b.ID equals ba.BookID
    join a in db.Authors on ba.AuthorID equals a.ID
    join bn in db.BookNumbers on b.ID equals bn.BookID
    where a.AuthorFirst == splitstring[1].ToString() && a.AuthorLast == splitstring[0].ToString()
    select new FillGrid
    {
    AuthorID = a.ID,
    Author = string.Format("{0} {1}", a.AuthorFirst, a.AuthorLast),
    Title = b.Title,
    Price = (decimal)b.Price,
    BookNumber = bn.SurrogateNumber,
    BookID = b.ID,
    Count = db.BookNumbers.Count(c => c.BookID == b.ID)
    }).ToList();

    I keep getting a conversion error... I also tried taking and doing query.tolist().firstordefault(); still no go any help would be great thanks

    D 1 Reply Last reply
    0
    • T tonyonlinux

      How would I do something like this

      var distinctBooks = query
      .GroupBy(x => x.BookID)
      .Select(x => x.FirstOrDefault());

      to this

      List<FillGrid> query = (from b in db.Books
      join ba in db.BookAuthors on b.ID equals ba.BookID
      join a in db.Authors on ba.AuthorID equals a.ID
      join bn in db.BookNumbers on b.ID equals bn.BookID
      where a.AuthorFirst == splitstring[1].ToString() && a.AuthorLast == splitstring[0].ToString()
      select new FillGrid
      {
      AuthorID = a.ID,
      Author = string.Format("{0} {1}", a.AuthorFirst, a.AuthorLast),
      Title = b.Title,
      Price = (decimal)b.Price,
      BookNumber = bn.SurrogateNumber,
      BookID = b.ID,
      Count = db.BookNumbers.Count(c => c.BookID == b.ID)
      }).ToList();

      I keep getting a conversion error... I also tried taking and doing query.tolist().firstordefault(); still no go any help would be great thanks

      D Offline
      D Offline
      Dan Mos
      wrote on last edited by
      #2

      your first query works. should work. so should the second one. eventually try

      var query = ...

      instead of List... what's the splitstring?

      T 1 Reply Last reply
      0
      • D Dan Mos

        your first query works. should work. so should the second one. eventually try

        var query = ...

        instead of List... what's the splitstring?

        T Offline
        T Offline
        tonyonlinux
        wrote on last edited by
        #3

        I realize the first and second queries work that's not the issue. The issue is how do i do the first one using the data from the second one? Basically i want to use the query's results and only return the distinct ones or the firstordefault() so basically what I'm trying to do is

        public class FillGrid
        {
        public int AuthorID {get; set;}
        public int BookNumber {get;set;}
        public int BookID { get; set;}
        public int Count {get; set;}
        public string Title { get; set; }
        public string Author { get; set; }
        public decimal Price { get; set; }
        public string Keywords { get; set; }

                                        public List<FillGrid>FillTheGrid(string treeviewText,string treeviewTag)
                                        {
                                            List <FillGrid>results = new List<FillGrid>();
        

        ........

        List<FillGrid> distinctresults = query.GroupBy(i => i.BookID).Select(i => i.FirstOrDefault());
        return distinctresults;
        </pre>

        but i keep getting the following error.
        <pre>
        Error 1 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<yetagain.DataOperations.FillGrid>' to 'System.Collections.Generic.List<yetagain.DataOperations.FillGrid>'. An explicit conversion exists (are you missing a cast?)

        D 1 Reply Last reply
        0
        • T tonyonlinux

          I realize the first and second queries work that's not the issue. The issue is how do i do the first one using the data from the second one? Basically i want to use the query's results and only return the distinct ones or the firstordefault() so basically what I'm trying to do is

          public class FillGrid
          {
          public int AuthorID {get; set;}
          public int BookNumber {get;set;}
          public int BookID { get; set;}
          public int Count {get; set;}
          public string Title { get; set; }
          public string Author { get; set; }
          public decimal Price { get; set; }
          public string Keywords { get; set; }

                                          public List<FillGrid>FillTheGrid(string treeviewText,string treeviewTag)
                                          {
                                              List <FillGrid>results = new List<FillGrid>();
          

          ........

          List<FillGrid> distinctresults = query.GroupBy(i => i.BookID).Select(i => i.FirstOrDefault());
          return distinctresults;
          </pre>

          but i keep getting the following error.
          <pre>
          Error 1 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<yetagain.DataOperations.FillGrid>' to 'System.Collections.Generic.List<yetagain.DataOperations.FillGrid>'. An explicit conversion exists (are you missing a cast?)

          D Offline
          D Offline
          Dan Mos
          wrote on last edited by
          #4

          well thats easy

          //either repalce
          List<FillGrid> distinctresults = query.GroupBy(i => i.BookID).Select(i => i.FirstOrDefault());
          //with
          List<FillGrid> distinctresults = query.GroupBy(i => i.BookID).Select(i => i.FirstOrDefault()).ToList();
          //or with
          var distinctresults = query.GroupBy(i => i.BookID).Select(i => i.FirstOrDefault());

          A LINQ query returns an IEnumerable by default and it does not get exectuted right away. Only when you do something with it. But if you call the .ToList() it will execute it and cache the results.

          T 1 Reply Last reply
          0
          • D Dan Mos

            well thats easy

            //either repalce
            List<FillGrid> distinctresults = query.GroupBy(i => i.BookID).Select(i => i.FirstOrDefault());
            //with
            List<FillGrid> distinctresults = query.GroupBy(i => i.BookID).Select(i => i.FirstOrDefault()).ToList();
            //or with
            var distinctresults = query.GroupBy(i => i.BookID).Select(i => i.FirstOrDefault());

            A LINQ query returns an IEnumerable by default and it does not get exectuted right away. Only when you do something with it. But if you call the .ToList() it will execute it and cache the results.

            T Offline
            T Offline
            tonyonlinux
            wrote on last edited by
            #5

            yeah i just did that before you replied thanks again :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups