473,490 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Linq. Aggregate

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
Sep 28 '08 #1
4 4707
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.
Sep 28 '08 #2
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
Sep 28 '08 #3
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.
Sep 29 '08 #4
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
Sep 29 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6087
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
1
2209
by: 0to60 | last post by:
Let's say we have your basic Invoices and InvoiceItems table. If we load this in with LINQ: var query = from i in db.Invoices select i; When I then loop through my invoices, if I wanna...
5
1949
by: shapper | last post by:
Hello, I have the following LINQ query: PostPaper paper = (from p in database.Posts where p.PostID == id select new PostPaper { Post = p, Tags = new List<Tag>( from pt in database.PostTags
2
1404
by: timor.super | last post by:
Hi all, I tryed to create a linq2xml request, but I wasn't able to. Can someone help me ? Here's the xml <datas> <date start="12/07/2008" end="31/08/2008">
2
1555
by: CSharper | last post by:
Yesterday I was working on a code where I need to split the parameters and create dictionary from it. The string is something like the following name key1=value1 key2=value2 here is the code...
1
1347
by: Mike P | last post by:
In the LINQ 101 examples, the Fold and EqualAll syntax no longer seems to be supported. Has the syntax been changed or is this functionality no longer available? *** Sent via Developersdex...
13
1787
by: Dan Tallent | last post by:
I have a query which I would like to convert to LINQ. I am having problems finding good examples or references that cover more complex queries. Here is the SQL command I would like to rewrite...
0
163
by: shapper | last post by:
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...
4
5315
by: =?Utf-8?B?UGFvbG8=?= | last post by:
I have a LINQ query which returns a sequence of rows all of which contains a decimal field (T_Amount) e.g. var trans = from trans in dataSet.Transaction where .... (filters) .... select new...
0
6974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7183
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6852
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4573
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.