How would this Linq query with Join be written as a Lambda?

entity-framework-6 lambda vb.net

Question

I am trying to learn how to do joins as lambdas and I have this linq query but cannot figure out how to do it as a lambda

From posts In db.be_Posts Join t In db.be_PostTag On posts.PostID 
Equals t.PostID Where t.Tag = tag Select posts.Title

The above Join works in LinqPad. And this is what I have so far as a lambda. I am also trying to put everything into the viewmodel.

db.be_Posts.OrderByDescending(Function(x) x.DateCreated).Join(db.be_PostTag, 
Function(p) p.PostID, Function(t) t.PostID, Function(p, t).Select(Function(p1) 
New be_PostsViewModel
With {.Id = 

I don't get an intellisense after .Id when I type either p, p1 or t. What am i doing wrong? Thank you

1
2
7/3/2017 12:45:54 PM

Accepted Answer

This part looks very weird (intended or typo?) :

....Function(p, t).Select(Function(p1)...

Something like this should work as far as I can see :

db.be_Posts.
    OrderByDescending(Function(x) x.DateCreated).
    Join(db.be_PostTag, _
            Function(p) p.PostID, _
            Function(t) t.PostID, _
            Function(p, t) New be_PostsViewModel With 
                            {
                                .Id = p.PostID,
                                ....
                                ....
                            })

And your LINQ query syntax can be translated to method syntax like this :

db.be_Posts.
    Join(db.be_PostTag, _
            Function(p) p.PostID, _
            Function(t) t.PostID, _
            Function(p, t) p.Title)
4
7/24/2014 4:05:02 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow