Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
SolvedPrivate

System.Linq.IQueryable<AnonymousType#1>'does not contain a definition for

Posted on 2014-01-27
3
Medium Priority
?
207 Views
Last Modified: 2016-02-10
Hi Experts!

Say you already know what the userID is.
and you want to get a count of 'Guest' and 'Member' for the user's church.

In SQL it looks like this:
DECLARE @ChurchID as Int

SELECT  @ChurchID = c.ChurchID
FROM ChurchMembers cm 
INNER JOIN Churches c ON	cm.ChurchID = c.ChurchID
WHERE cm.UserID = 2

SELECT r.Name, COUNT(r.Name) [Count]
FROM users u 
INNER JOIN ChurchMembers cm ON u.UserID = cm.UserID
INNER JOIN Churches c ON	cm.ChurchID = c.ChurchID
INNER JOIN UserRoles ur ON u.UserID = ur.UserID
INNER JOIN Roles r ON ur.RoleID = r.RoleID
WHERE c.ChurchID = @ChurchID
AND r.Name IN ('Guest', 'Member')
GROUP BY r.Name

Open in new window

The result is something like this:
Name         Count 
------------ ------
Guest        25
Member       1

Open in new window

Trying to do it in LINQ:
var myChurch = from cm in db.ChurchMembers
                join c in db.Churches
                on cm.ChurchID equals c.ChurchID
                where cm.UserID == myUser.UserID
                select new
                {
                    ChurchID =  c.ChurchID
                };

Open in new window

This is the part got stuck on:
var data = from cm in db.ChurchMembers
            join c in db.Churches
            on cm.ChurchID equals c.ChurchID
            join ur in db.UserRoles
            on cm.UserID  equals ur.UserID
            join r in db.Roles
            on ur.RoleID  equals r.RoleID
            where c.ChurchID == myChurch.ChurchID

Open in new window


The error:
System.Linq.IQueryable<AnonymousType#1>'does not contain a definition for 'ChurchID' and
no extension method 'ChurchID' accepting of type 'System.Linq.IQueryable<AnonymousType#1>'
cound be found (are you missing a using directive or an assembly reference?)
Can you please help so that the data contains:
Name         Count 
------------ ------
Guest        25
Member       1

Open in new window

TIA!
0
Comment
Question by:allanau20
[X]
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
3 Comments
 
LVL 11

Expert Comment

by:Kusala Wijayasena
ID: 39811902
Use Linqer and try to convert your SQL to LINQ (using trial version you can do it)
0
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 2000 total points
ID: 39811913
A thing you need to understand here is that 'myChurch' is not a flat object, but a collection of objects. In fact it is a collection of Anonymous Objects, which all have a unique property called ChurchID.

Having said that, myChurch.ChurchID is wrong, as the collection doesn't have this property (only the objects within the collection have this property).

You have two options that depend on your needs:

1. Make sure your first query return a single object and not a collection of them. This is by calling one of:
First
This will get you the first object of the collection. It will throw an exception if there is none.
FirstOrDefault
This will get you the first object of the collection. It will not throw an exception if there is none.
Single
This will get you the only object of the collection. It will throw an exception if there is not just one and only one object in the collection.
SingleOrDefault
This will get you the only object of the collection. It will throw an exception if there is more than one objects in the collection.
Last
This will get you the last object of the collection. It will throw an exception if there is none.
LastOrDefault
This will get you the last object of the collection. It will not throw an exception if there is none.

For this you should have your myChurch declaration as:

var myChurch = (from cm in ChurchMembers
           join c in Churches
           on cm.ChurchID equals c.ChurchID
           where cm.UserID == 1
           select new
           {
               ChurchID =  c.ChurchID
           }).l.SingleOrDefault();

Open in new window


2. If you do need a collection of churches to be returned in the first query, then you need to modify your second one to act accordingly:

var data = from cm in ChurchMembers
            join c in Churches
            on cm.ChurchID equals c.ChurchID
            join ur in UserRoles
            on cm.UserID  equals ur.UserID
            join r in Roles
            on ur.RoleID  equals r.RoleID
            where myChurch.Contains(c.ChurchID)

Open in new window


That way you check if c.ChurchID is one of the items in the collection myChurch, which is an IEnumberable<int>.

Giannis
0
 
LVL 5

Author Closing Comment

by:allanau20
ID: 39813400
Thank you jyparask for the clear explanation; I understand now.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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.​
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

721 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