473,503 Members | 10,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linq. Please, need help.

Hello,

I am using the following:

PostPaper paper = (from p in database.Posts
where p.PostID == id
select new PostPaper {
Post = p,
Tags = (from pt in database.PostsTags
join t in database.Tags on pt.TagID
equals t.TagID
where pt.PostID == id
orderby t.Name
select t).ToList()
}).SingleOrDefault();

PostPaper has also another property named TagsCSV which is the same
has Tags but in a CSV format so I have:

paper.TagsCSV = string.Join(", ", paper.Tags.Select(t =>
t.Name).ToArray());

The problem is that sometimes in also get a list of papers:

viewData.PostsPapers = (from p in database.Posts
orderby p.UpdatedAt descending
where p.IsPublished == true
select new PostPaper {
Post = p,
Tags = (from pt in database.PostsTags
join t in database.Tags on
pt.TagID equals t.TagID
where pt.PostID == p.PostID
orderby t.Name
select t).ToList()
}).ToPagedList(page.HasValue ?
page.Value - 1 : 0, PageSize);

Where PostPapers is an IPagedList<PollPaper>.

How can I create the TagsCSV for each PostPaper in viewData
PostPapers?

I tried to integrate the TagsCSV creation in select new PostPaper but
I wasn't able to do.

Thanks,
Miguel
Sep 27 '08 #1
1 1081
shapper wrote:
I am using the following:

PostPaper paper = (from p in database.Posts
where p.PostID == id
select new PostPaper {
Post = p,
Tags = (from pt in database.PostsTags
join t in database.Tags on pt.TagID
equals t.TagID
where pt.PostID == id
orderby t.Name
select t).ToList()
}).SingleOrDefault();
While this will work just fine, you may want to consider using the designer
to generate entity sets and associations. This will take care of the joins
for you, allowing you to write much more concise code:

from p in database.Posts
where p.PostID == id
select new PostPaper ( Post = p, Tags = p.Tags.OrderBy(t =>
t.Name).ToList() };

Consult the help for more information on associations.
PostPaper has also another property named TagsCSV which is the same
has Tags but in a CSV format so I have:

paper.TagsCSV = string.Join(", ", paper.Tags.Select(t =>
t.Name).ToArray());

The problem is that sometimes in also get a list of papers:

viewData.PostsPapers = (from p in database.Posts
orderby p.UpdatedAt descending
where p.IsPublished == true
select new PostPaper {
Post = p,
Tags = (from pt in database.PostsTags
join t in database.Tags on
pt.TagID equals t.TagID
where pt.PostID == p.PostID
orderby t.Name
select t).ToList()
}).ToPagedList(page.HasValue ?
page.Value - 1 : 0, PageSize);

Where PostPapers is an IPagedList<PollPaper>.

How can I create the TagsCSV for each PostPaper in viewData
PostPapers?

I tried to integrate the TagsCSV creation in select new PostPaper but
I wasn't able to do.
Again, the continuous selecting of tags belonging to posts suggests you want
an automatically managed entity set for that. You can do without, though:

from p in database.Posts
...
let tags = (from pt in database.PostTags...
let tagsCsv = string.Join(", ", tags.Select(t =t.Name).ToArray());
select new PostPaper { Post = p, Tags = tags.To, TagsCSV =
tagsCsv.ToArray }...

The "let" clause allows you to store arbitrary intermediate results. It's
very convenient for enhancing the readability of big queries like these.

--
J.
Sep 27 '08 #2

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

Similar topics

28
16350
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
6
1495
by: =?Utf-8?B?TmVkaW0=?= | last post by:
I am starting a new web application and I was planning on using linq but i have a bunch of problems. most forms will display some data from the database, but there always has to be a filtering...
4
2173
by: =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post by:
We’re storing our main entity in an insert only table which stores the history of past revisions, but we’re facing problems with storing this history as LINQ will only update the entity, and...
9
2246
by: Cirene | last post by:
I'm about to begin a brand new, big, ASP.NET project (using 3.5 .net fw), VS 2008. I'm using MySQL as the backend (customer request.) I have absolutely no experience with LINQ and/or the Entity...
3
12415
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
0
7095
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
7294
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
7361
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...
0
7470
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
5602
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,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4693
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
3183
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...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.