471,354 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

Linq. String

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
Aug 21 '08 #1
5 1883
shapper wrote:
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)

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 21 '08 #2
On Aug 21, 4:11*pm, Martin Honnen <mahotr...@yahoo.dewrote:
shapper wrote:
* * * * * * * * * * * * * *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)

--

* * * * Martin Honnen --- MVP XML
* * * *http://JavaScript.FAQTs.com/
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
Aug 21 '08 #3
On Aug 21, 11:56*pm, shapper <mdmo...@gmail.comwrote:
On Aug 21, 4:11*pm, Martin Honnen <mahotr...@yahoo.dewrote:
shapper wrote:
* * * * * * * * * * * * * *Tags = new List<Tag>(
* * * * * * * * * * * * * * *from pt indatabase.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)
--
* * * * Martin Honnen --- MVP XML
* * * *http://JavaScript.FAQTs.com/

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
Aug 22 '08 #4
shapper wrote:
I think Aggregate does not work with Linq. Any idea of how to solve
this?
It works fine for me:

List<stringlist1 = 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?
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 22 '08 #5
Martin Honnen wrote:
shapper wrote:
>I think Aggregate does not work with Linq. Any idea of how to solve
this?

It works fine for me:

List<stringlist1 = 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 <dbprovider 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#)
------------------------------------------------------------------------
Aug 23 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by Marc Gravell | last post: by
4 posts views Thread by BeSharp | last post: by
4 posts views Thread by Arthur Dent | last post: by
8 posts views Thread by Andy | last post: by
reply views Thread by =?Utf-8?B?SHlwZXJjb2Rlcg==?= | last post: by
4 posts views Thread by =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post: by
14 posts views Thread by thj | last post: by
21 posts views Thread by hrishy | last post: by
1 post views Thread by Dean Slindee | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.