Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

linq groupjoin using lambda with a where clause

Posted on 2013-01-13
4
676 Views
Last Modified: 2013-11-05
I need to add a join using Lambda if I have a further parameter available that will also be used in a where clause.

This is my original linq code, it works just fine...

    var tagList =   from t in dc.Tags
    join b in dc.Businesses on t.BusinessId equals b.BusinessId
            where t.IsActive == true
            where b.IsActive == true
            orderby t.AdImage descending
            select new TagItem
            {
                    tagName = t.Name.Replace("\"", ""),
                    tagImage = tagImagePath + t.AdImage.Replace("\"", ""),
                    tagDescription = t.Description.Replace("\"", "")
            };

My problem is I'm not sure of the exact format for adding a new object [table] MemberTagLysts using Lambda and then how the where clause should be created when I have a lystId available...

This is not quite right....

            if (!string.IsNullOrEmpty(lystId))
            {
                            tagList = (IQueryable<TagItem>)tagList.GroupJoin(dc.MemberTagLysts, a => a.tagId, b => b.TagId, (a, b) => new { a, b });
            }
0
Comment
Question by:Rob Gaudet
  • 3
4 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 38774007
can u explain what kind of join you wish to apply with MemberTagLysts table?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38774017
first u need to add tagId in your initial linq query:
.
.
.select new TagItem
            {
tagId = t.tagId,
                    tagName = t.Name.Replace("\"", ""),
                    tagImage = tagImagePath + t.AdImage.Replace("\"", ""),
                    tagDescription = t.Description.Replace("\"", "")
            };

Open in new window


now try run this:
   tagList = (IQueryable<TagItem>)tagList.GroupJoin(dc.MemberTagLysts, a => a.tagId, b => b.TagId, (a, b) => new { a, b });

Open in new window

0
 

Author Comment

by:Rob Gaudet
ID: 38779233
Sorry, tagid was in the initial query, I shortened the values to save space and pulled it out..

Your suggestion...

tagList = (IQueryable<TagItem>)tagList.GroupJoin(dc.MemberTagLysts, a => a.tagId, b => b.TagId, (a, b) => new { a, b });

....returned this error.

Unable to cast object of type 'System.Data.Linq.DataQuery`1[<>f__AnonymousType6`2[TagItem,System.Collections.Generic.IEnumerable`1[MemberTagLyst]]]' to type 'System.Linq.IQueryable`1[TagItem]'.
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 38779580
just
var group = tagList.GroupJoin(dc.MemberTagLysts, a => a.tagId, b => b.TagId, (a, b) => new { a, b });

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question