Linq > Group

S

shapper

Hello,

I have the following Linq query:

List<PostsTag> insert = (from t in
(from t in database.Tags
join p in paper.Tags on t.Name
equals p.Name
select t).ToList()
select new PostsTag {
PostID = paper.Post.PostID,
TagID = t.TagID
}).ToList();

How can I convert this query to use Linq's Group instead of using the
sub query?

Tags table has 2 fields: TagID and Name
PostTags table has 2 fields PostId and TagID

Thanks,
Miguel
 
F

Frans Bouma [C# MVP]

shapper said:
Hello,

I have the following Linq query:

List<PostsTag> insert = (from t in
(from t in database.Tags
join p in paper.Tags on t.Name
equals p.Name
select t).ToList()
select new PostsTag {
PostID = paper.Post.PostID,
TagID = t.TagID
}).ToList();

How can I convert this query to use Linq's Group instead of using the
sub query?

Tags table has 2 fields: TagID and Name
PostTags table has 2 fields PostId and TagID

Miguel, you ask about 4, 5 linq questions a day. Now, I'm not sure if
you're a professional developer or just a guy with a hobby project, but
either way, isn't it more productive for you to simply get a good book
about the subject, work your way through it and then apply that
knowledge to your work with linq? E.g. get "Linq in Action" from
amazon.com and use the knowledge in there to answer your own questions.
We can step in every day and help you with the question(s) you have that
day, but I get the feeling you keep running into the same problems over
and over with slightly different parameters, i.o.w.: you understood the
trick to solve yesterday's problem, but didn't completely
understand/grasp the knowledge/idea behind the solution of yesterday's
questions to answer today's questions.

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#)
------------------------------------------------------------------------
 
S

shapper

        Miguel, you ask about 4, 5 linq questions a day. Now, I'mnot sure if
you're a professional developer or just a guy with a hobby project, but
either way, isn't it more productive for you to simply get a good book
about the subject, work your way through it and then apply that
knowledge to your work with linq? E.g. get "Linq in Action" from
amazon.com and use the knowledge in there to answer your own questions.
We can step in every day and help you with the question(s) you have that
day, but I get the feeling you keep running into the same problems over
and over with slightly different parameters, i.o.w.: you understood the
trick to solve yesterday's problem, but didn't completely
understand/grasp the knowledge/idea behind the solution of yesterday's
questions to answer today's questions.

                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#)
------------------------------------------------------------------------

Hi Frank,

4 to 5 posts a day is an exaggeration ... maybe on a specific day ...

What I am doing is converting all SQL code I have to LINQ and this is
lately I have been posting a few post on LINQ.

For reference, I am a free lancer, and I am using MSDN Linq 101
Examples ...

In this post, as in many others, I am more looking for a confirmation
if what I am doing is the best way to do it:

I have 2 tables:

Both Tags and paper.Tags are List<Tag>.
Tag is a class with two properties: TagID and Name.

In Tags both TagID and Name are defined.
In paper.Tags only Name is defined.

So I need to get the TagID's from Tags on the Names I have in
paper.Tags and create the PostsTags.

This is working:
List<PostsTag> insert = (from t in
(from t in database.Tags
join p in paper.Tags on t.Name
equals p.Name
select t).ToList()
select new PostsTag {
PostID = paper.Post.PostID,
TagID = t.TagID
}).ToList();

But to be honest it seems the same as:

List<PostsTag> tags = (from t in database.Tags
join pt in paper.Tags on t.Name equals
pt.Name
select new PostsTag {
PostID = paper.Post.PostID,
TagID = t.TagID
}).ToList();

I don't think I need the subquery or groupby because I am making a one
to one relation between Tags and paper.Tags.

This is what I am trying to check.

Thank You,
Miguel
paper.Tags >
 

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