Solved

linq groupjoin using lambda with a where clause

Posted on 2013-01-13
4
648 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now