Go Premium for a chance to win a PS4. Enter to Win

x
?
SolvedPrivate

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

Posted on 2014-01-27
3
Medium Priority
?
217 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Screencast - Getting to Know the Pipeline

971 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