Solved

linq groupjoin using lambda with a where clause

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# HTTP GET method sample code 3 41
Eagerly loading related objects in entity framework 5 26
SQL Exceptions 3 39
SQL Statement to Update Email Domain 2 23
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

11 Experts available now in Live!

Get 1:1 Help Now