Linq. String

S

shapper

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
join t in database.Tags on pt.TagID
equals t.TagID
where pt.PostID == p.PostID
select t).ToString()
}).SingleOrDefault();

Tags should be a string with CSV values. So I added it the ToString()
at the end. However it is returning a List.

What am I doing wrong?

Thanks,
Miguel
 
M

Martin Honnen

shapper said:
Tags = new List<Tag>(
from pt in database.PostTags
join t in database.Tags on pt.TagID
equals t.TagID
where pt.PostID == p.PostID
select t).ToString()
}).SingleOrDefault();

Tags should be a string with CSV values. So I added it the ToString()
at the end. However it is returning a List.

What am I doing wrong?

If you want a string with comma separated values then you could try
Tags = (from pt in database.PostTags
join t in database.Tags on pt.TagID
equals t.TagID
where pt.PostID == p.PostID
select t).Aggregrate((a, b) => a + "," + b)
 
S

shapper

If you want a string with comma separated values then you could try
   Tags =                              (from pt in database.PostTags
                              join t in database.Tags on pt.TagID
equals t.TagID
                              where pt.PostID == p.PostID
                              select t).Aggregrate((a, b) => a + "," + b)

I get an error on the Aggregrate function:
'System.Collections.Generic.List<Models.Tag>' does not contain a
definition for 'Aggregrate' and no extension method 'Aggregrate'
accepting a first argument of type
'System.Collections.Generic.List<Models.Tag>' could be found (are you
missing a using directive or an assembly reference?)

Do you know what is wrong?

Thanks,
Miguel
 
S

shapper

I get an error on the Aggregrate function:
'System.Collections.Generic.List<Models.Tag>' does not contain a
definition for 'Aggregrate' and no extension method 'Aggregrate'
accepting a first argument of type
'System.Collections.Generic.List<Models.Tag>' could be found (are you
missing a using directive or an assembly reference?)

Do you know what is wrong?

Thanks,
Miguel

I think Aggregate does not work with Linq. Any idea of how to solve
this?

Thanks,
Miguel
 
M

Martin Honnen

shapper said:
I think Aggregate does not work with Linq. Any idea of how to solve
this?

It works fine for me:

List<string> list1 = new List<string>() { "1", "2", "3" };
Console.WriteLine(list1.Aggregate((a, b) => a + "," + b));

Can you post the definitions of the types you are using?
 
F

Frans Bouma [C# MVP]

Martin said:
It works fine for me:

List<string> list1 = new List<string>() { "1", "2", "3" };
Console.WriteLine(list1.Aggregate((a, b) => a + "," + b));

Can you post the definitions of the types you are using?

Youor query runs in memory, his in the DB. The 'Aggregate' extension
method doesn't have a DB equivalent so every Linq to <db> provider will
give up on it, unless it can convert it to an inmemory method call,
which isn't the case with Aggregate stuff.

TS should pass the nested query result to a method, concat there, and
return teh result and use that as a value.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top