Hello,
I have the following Linq query:
var q = (from p in database.Posts
join pt in database.PostsTags 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.Models.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 4 4565
shapper wrote:
I have the following Linq query:
var q = (from p in database.Posts
join pt in database.PostsTags 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.Models.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).ToArray())
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.
On Sep 29, 12:18*am, Jeroen Mostert <jmost...@xs4all.nlwrote:
shapper wrote:
I have the following Linq query:
* * * var q = (from p in database.Posts
* * * * * * * * * join pt in database.PostsTags 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.Models.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).ToArray())
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
shapper wrote:
On Sep 29, 12:18 am, Jeroen Mostert <jmost...@xs4all.nlwrote:
>shapper wrote:
>>I have the following Linq query: var q = (from p in database.Posts join pt in database.PostsTags 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.Models.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).ToArray())
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.
On Sep 29, 12:57*am, Jeroen Mostert <jmost...@xs4all.nlwrote:
shapper wrote:
On Sep 29, 12:18 am, Jeroen Mostert <jmost...@xs4all.nlwrote:
shapper wrote: I have the following Linq query: * * * var q = (from p in database.Posts * * * * * * * * * join pt in database.PostsTags 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.Models.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).ToArray())
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 This discussion thread is closed Replies have been disabled for this discussion. Similar topics |
1 post
views
Thread by 0to60 |
last post: by
|
5 posts
views
Thread by shapper |
last post: by
|
2 posts
views
Thread by timor.super |
last post: by
|
2 posts
views
Thread by CSharper |
last post: by
|
1 post
views
Thread by Mike P |
last post: by
|
13 posts
views
Thread by Dan Tallent |
last post: by
|
reply
views
Thread by shapper |
last post: by
|
4 posts
views
Thread by =?Utf-8?B?UGFvbG8=?= |
last post: by
| | | | | | | | | | |