Avatar of Allan
AllanFlag for United States of America

asked on 

LINQ - query for pair values

Hi Experts!

Thanks for reading this.

Say there are three tables:
PostTags:
   PostTagID (PK)
   TagName

PostTagPairs
   PostTagPairID (PK)
   PostID (FK)
   PostTagID (FK)

Posts
   PostID (PK)
   PostTitle

Open in new window

The following query will return TagName associated with Posts:
var myPost = (from bp in db.Posts
              where bp.PostID == postId
              select new
              {
                  bp.PostTitle,
                  tags = from p in bp.PostTagPairs select p.PostTag.TagName
              }).SingleOrDefault();

Open in new window

Question is how do you get the query above to return PostTagID along with TagName and iterate it in a for loop?
foreach(?? in myPost.tags)
       {
         ??
       } 

Open in new window

TIA!
ASP.NETC#.NET Programming

Avatar of undefined
Last Comment
Fernando Soto
SOLUTION
Avatar of Ioannis Paraskevopoulos
Ioannis Paraskevopoulos
Flag of Greece image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Hi allanau20;

The following should give you what you want.

var myPost = (from bp in db.Posts
              where bp.PostID == postId
              select new
              {
                  bp.PostTitle,
                  tags = (from p in bp.PostTagPairs 
                          select new 
                          { 
                              TagName = p.PostTag.TagName, 
                              PostTagID = p.PostTag.PostTagID
                          }).ToList() 
              }).SingleOrDefault();

foreach( var pt in myPost.tags )
{
    Console.WriteLine("The TagName is : {0} The PostTagID is : {1}", pt.TagName, pt.PostTagID);
}

Open in new window

Avatar of Allan
Allan
Flag of United States of America image

ASKER

thanks guys; let me try it now and get back to you...
Avatar of Allan
Allan
Flag of United States of America image

ASKER

Hi FernandoSoto,

Got this error:

{"LINQ to Entities does not recognize the method 'System.Collections.Generic.List`1[<>f__AnonymousType0`2[System.String,System.Int32]] ToList[<>f__AnonymousType0`2](System.Collections.Generic.IEnumerable`1[<>f__AnonymousType0`2[System.String,System.Int32]])' method, and this method cannot be translated into a store expression."}


Any ideas?
Avatar of Allan
Allan
Flag of United States of America image

ASKER

Hi jyparask,

looks like it doesn't recognize temp:

User generated imageany ideas?
Try this one

            var query = from t in PostTags
                        join pt in PostTagPairs
                        on t.PostTagId equals pt.PostTagId into
                        select new
                        {
                               p.PostTitle,
                               t.TagName
                        };

Giannis

PS what is your environment?
Avatar of Allan
Allan
Flag of United States of America image

ASKER

sorry Giannis.

This is razor on MVC3 with EF 4.1, .net 4.0; i'll try it now.
Avatar of Allan
Allan
Flag of United States of America image

ASKER

Hi Giannis.

for this --->  p.PostTitle,

I don't see where p (Posts) is defined?
also, would like to pull PostTagID (along with TagName). thx!
Do you use temp somewhere else in your code? Try renaming temp on my initial post to something else.
Hey,  it says tenp. This is a misspell. Change the error to temp.

Giannis
Avatar of Allan
Allan
Flag of United States of America image

ASKER

Thanks. I'm on the road. I'll try it again in 4hrs.
ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Allan
Allan
Flag of United States of America image

ASKER

Thanks FernandoSoto; that works.

I'll want to try out yparask solution if you don't mind. thx.
Hi,

If you like, you may also check an article i wrote for joining in LINQ, that has just been published.

https://www.experts-exchange.com/Programming/Languages/.NET/LINQ/A_11369-Joining-in-LINQ.html

If you wish feel free to leave a comment.

Giannis
Avatar of Allan
Allan
Flag of United States of America image

ASKER

Thanks Giannis your solution would work too.
I guess if I wanted to PostTitle (outside the for loop) I cannot get to it like this?
var Title = query.PostTitle

Anyways, appreciate your help and will provide comment to your article later this week.
Not a problem allanau20, glad I could help.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo