Linq. Aggregate

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    #1

    Linq. Aggregate

    Hello,

    I have the following Linq query:

    var q = (from p in database.Posts
    join pt in database.PostsT ags on p.PostID equals
    pt.PostID
    join t in database.Tags on pt.TagID equals t.TagID
    group t by p into pt
    select new PostPaper {
    Post = pt.Key,
    Tags = pt.ToList(),
    TagsCSV = pt.Aggregate((a , b) =a.Name + ", " +
    b.Name)
    }).ToPagedList( page.HasValue ? page.Value - 1 : 0,
    ListPageSize);

    I am getting an error on the aggregate function:
    Cannot implicitly convert type 'string' to 'BonsAlunos.Mod els.Tag'

    Basically, what I am trying to do is build TagsCSV with the Name
    property of all tags from Tags list separated by commas ... any idea
    of how to do this?

    Thanks,
    Miguel
  • Jeroen Mostert

    #2
    Re: Linq. Aggregate

    shapper wrote:
    I have the following Linq query:
    >
    var q = (from p in database.Posts
    join pt in database.PostsT ags on p.PostID equals
    pt.PostID
    join t in database.Tags on pt.TagID equals t.TagID
    group t by p into pt
    select new PostPaper {
    Post = pt.Key,
    Tags = pt.ToList(),
    TagsCSV = pt.Aggregate((a , b) =a.Name + ", " +
    b.Name)
    }).ToPagedList( page.HasValue ? page.Value - 1 : 0,
    ListPageSize);
    >
    I am getting an error on the aggregate function:
    Cannot implicitly convert type 'string' to 'BonsAlunos.Mod els.Tag'
    >
    While you can use .Aggregate() for this, it's more trouble than it's worth
    (removing the final, unnecessary comma can't be done with an inline
    expression). Just use string.Join():

    TagsCSV = string.Join(", ", pt.Select(t =t.Name).ToArra y())

    And I'm getting a strong sense of deja vu here, because you used this exact
    expression in a post made not 24 hours ago in this newsgroup. It was good
    then, it's still good now. :-)

    --
    J.

    Comment

    • shapper

      #3
      Re: Linq. Aggregate

      On Sep 29, 12:18 am, Jeroen Mostert <jmost...@xs4al l.nlwrote:
      shapper wrote:
      I have the following Linq query:
      >
            var q = (from p in database.Posts
                        join pt in database.PostsT ags on p.PostID equals
      pt.PostID
                        join t in database.Tags on pt.TagIDequals t.TagID
                        group t by p into pt
                        select new PostPaper {
                          Post = pt.Key,
                          Tags = pt.ToList(),
                          TagsCSV = pt.Aggregate((a , b)=a.Name + ", " +
      b.Name)
                        }).ToPagedList( page.HasValue ? page..Value - 1 : 0,
      ListPageSize);
      >
      I am getting an error on the aggregate function:
      Cannot implicitly convert type 'string' to 'BonsAlunos.Mod els.Tag'
      >
      While you can use .Aggregate() for this, it's more trouble than it's worth
      (removing the final, unnecessary comma can't be done with an inline
      expression). Just use string.Join():
      >
         TagsCSV = string.Join(", ", pt.Select(t =t.Name).ToArra y())
      >
      And I'm getting a strong sense of deja vu here, because you used this exact
      expression in a post made not 24 hours ago in this newsgroup. It was good
      then, it's still good now. :-)
      >
      --
      J.
      Yes, you are right!

      But I saw something like this using Aggregate so I though it was a
      better way to do this.

      So I will keep String.Join.

      Thanks,
      Miguel

      Comment

      • Jeroen Mostert

        #4
        Re: Linq. Aggregate

        shapper wrote:
        On Sep 29, 12:18 am, Jeroen Mostert <jmost...@xs4al l.nlwrote:
        >shapper wrote:
        >>I have the following Linq query:
        >> var q = (from p in database.Posts
        >> join pt in database.PostsT ags on p.PostID equals
        >>pt.PostID
        >> join t in database.Tags on pt.TagID equals t.TagID
        >> group t by p into pt
        >> select new PostPaper {
        >> Post = pt.Key,
        >> Tags = pt.ToList(),
        >> TagsCSV = pt.Aggregate((a , b) =a.Name + ", " +
        >>b.Name)
        >> }).ToPagedList( page.HasValue ? page.Value - 1 : 0,
        >>ListPageSize) ;
        >>I am getting an error on the aggregate function:
        >>Cannot implicitly convert type 'string' to 'BonsAlunos.Mod els.Tag'
        >While you can use .Aggregate() for this, it's more trouble than it's worth
        >(removing the final, unnecessary comma can't be done with an inline
        >expression). Just use string.Join():
        >>
        > TagsCSV = string.Join(", ", pt.Select(t =t.Name).ToArra y())
        >>
        >And I'm getting a strong sense of deja vu here, because you used this exact
        >expression in a post made not 24 hours ago in this newsgroup. It was good
        >then, it's still good now. :-)
        >>
        >
        Yes, you are right!
        >
        But I saw something like this using Aggregate so I though it was a
        better way to do this.
        >
        Actually, it depends. If you expect that there will be a great number of
        PostPaper objects and .TagsCSV will only be evaluated sporadically, then
        using .Aggregate() may win out as its execution is deferred (unlike
        String.Join). This would be pretty unusual, though.

        On the other hand, if you expect .TagsCSV to be evaluated for every item,
        then .Aggregate() is certainly not better here. It essentially amounts to
        concatenating strings in a loop, which is what we're always told not to do
        (as it produces a lot of string garbage). Although creating the array with
        names has overhead too, it still scales better.

        Using an explicit StringBuilder is more efficient still, but that's hardly
        worth it.

        --
        J.

        Comment

        • shapper

          #5
          Re: Linq. Aggregate

          On Sep 29, 12:57 am, Jeroen Mostert <jmost...@xs4al l.nlwrote:
          shapper wrote:
          On Sep 29, 12:18 am, Jeroen Mostert <jmost...@xs4al l.nlwrote:
          shapper wrote:
          >I have the following Linq query:
          >      var q = (from p in database.Posts
          >                  join pt in database.PostsT ags on p.PostID equals
          >pt.PostID
          >                  join t in database.Tags on pt.TagID equals t.TagID
          >                  group t by p into pt
          >                  select new PostPaper {
          >                    Post = pt.Key,
          >                    Tags = pt.ToList(),
          >                    TagsCSV = pt.Aggregate((a , b) =a.Name + ", " +
          >b.Name)
          >                  }).ToPagedList( page.HasValue ? page.Value - 1 : 0,
          >ListPageSize );
          >I am getting an error on the aggregate function:
          >Cannot implicitly convert type 'string' to 'BonsAlunos.Mod els.Tag'
          While you can use .Aggregate() for this, it's more trouble than it's worth
          (removing the final, unnecessary comma can't be done with an inline
          expression). Just use string.Join():
          >
             TagsCSV = string.Join(", ", pt.Select(t =t.Name).ToArra y())
          >
          And I'm getting a strong sense of deja vu here, because you used this exact
          expression in a post made not 24 hours ago in this newsgroup. It was good
          then, it's still good now. :-)
          >
          Yes, you are right!
          >
          But I saw something like this using Aggregate so I though it was a
          better way to do this.
          >
          Actually, it depends. If you expect that there will be a great number of
          PostPaper objects and .TagsCSV will only be evaluated sporadically, then
          using .Aggregate() may win out as its execution is deferred (unlike
          String.Join). This would be pretty unusual, though.
          >
          On the other hand, if you expect .TagsCSV to be evaluated for every item,
          then .Aggregate() is certainly not better here. It essentially amounts to
          concatenating strings in a loop, which is what we're always told not to do
          (as it produces a lot of string garbage). Although creating the array with
          names has overhead too, it still scales better.
          >
          Using an explicit StringBuilder is more efficient still, but that's hardly
          worth it.
          >
          --
          J.
          From your description String.Join seems the best solution.

          Thank You,
          Miguel

          Comment

          Working...