Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Private
  • Views: 222
  • Last Modified:

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

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
allanau20
Asked:
allanau20
1 Solution
 
Kusala WijayasenaSoftware EngineerCommented:
Use Linqer and try to convert your SQL to LINQ (using trial version you can do it)
0
 
Ioannis ParaskevopoulosCommented:
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
 
allanau20Author Commented:
Thank you jyparask for the clear explanation; I understand now.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now