SolvedPrivate

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

Posted on 2014-01-27
3
178 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
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access to class from any project within a solution. 6 14
C# parent child form 5 16
Coding C# in Linux 8 21
Variable Event ? 3 22
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.​
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

16 Experts available now in Live!

Get 1:1 Help Now