SolvedPrivate

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

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Image(7) 1 37
cs.Designer Issue 2 19
Why don't I see this table in EDMX file? 2 20
Fixed Length SQL Query Question 3 14
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

19 Experts available now in Live!

Get 1:1 Help Now