O'Reilly Forums: Error - Cannot Convert Lambda Expression To Type 'string' Because Is Not A Delegate Type - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Error - Cannot Convert Lambda Expression To Type 'string' Because Is Not A Delegate Type Anonymous types as references

#1 User is offline   caof2005 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 26-December 11

Posted 26 December 2011 - 03:49 PM

Hello Folks,

I´m trying to follow each of the exercises in the book, and now in "chapter 4: Exploring LINQ to Entities in Greater Depth" .

I´m trying to compile the following block of code:


private static void QueryContacts6()
{
using (var context = new SampleEntities())
{
var contacts = from c in context.Contacts
select new {
c.FirstName,
c.LastName,
StreetsCities = from a in c.Addresses
select new { a.Street1, a.City }
};
}
}


when I try to compile what I´m getting are a couple of errors:

1 - Error 1 Cannot convert lambda expression to type 'string' because it is not a delegate type
//-- This is marked in the beginning of the select statement

2 - Error 2 'Chapter2ConsoleApp.Contact' does not contain a definition for 'Addresses'
and no extension method 'Addresses' accepting a first argument of type
'Chapter2ConsoleApp.Contact' could be found
(are you missing a using directive or an assembly reference?)
//-- This is marked in the "c.Address" portion of the inner select statement.

I already included the "using System.Linq;" , "using System.Data.EntityClient;" and "using System.Data;" at the beginning of the code file and already checked that I have a defined a reference to System.Data.DataSetExtensions.

Any idea or hint on what I´m missing or not having in mind would be very appreciated.

Thanks in advance
Carlos
0

#2 User is offline   WhiskeyRomeoLima 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 30
  • Joined: 29-October 11

Posted 26 December 2011 - 04:00 PM

Does your using statements look like this?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text
0

#3 User is offline   JulieLerman 

  • Advanced Member
  • PipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 334
  • Joined: 17-September 08

Posted 26 December 2011 - 06:19 PM

Hi, I'm looking at your code and looking at my code file open in visual studio which has no problem compiling. I even commented out my code and copy/pasted yours into the file and still have no problem.

I commented out a few of my using namespace directives but could not get your error.

So...are you sure your model is correct? Did intellisense help you build this query or did you copy paste from a digital copy? I suspect that the message saying that context does not have an Addresses property may be telling you exactly what the problem is.

Julie
0

#4 User is offline   caof2005 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 26-December 11

Posted 27 December 2011 - 08:42 AM

View PostWhiskeyRomeoLima, on 26 December 2011 - 04:00 PM, said:

Does your using statements look like this?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text


Yes as a matter of fact this is my complete listing:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data.EntityClient;
using System.Data;

namespace Chapter2ConsoleApp
{
class Program
{
static void Main(string[] args)
{
QueryContacts6();
}

//*****************************************************************************************//
// Following block doesn´t compile at all.
//*****************************************************************************************//
private static void QueryContacts6()
{

using (var context = new SampleEntities())
{
var contacts = from c in context.Contacts
select new
{
c.FirstName,
c.LastName,
StreetsCities = from a in c.Addresses
select new { a.Street1, a.City }
};
}
}
}
}

I have references for System.Data, System.Data.DataSetExtensions, System.Data.Entity

My model was generated successfully => I was able to generate Model1.edmx with Contact, Address and vOfficeAddresses entities.

I could also generate the UML Class diagram (ClassDiagram1.cd) with SampleEntities, Contact, Address, and vOfficeAddresses clases.

So I suppose the problem does not to be related with the process of generating the model but with some syntactical or semantic stuff that I'm forgetting or not doing correctly.

What is your opinion?

Regards
0

#5 User is offline   JulieLerman 

  • Advanced Member
  • PipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 334
  • Joined: 17-September 08

Posted 27 December 2011 - 08:45 AM

Do you h ave a relationship between Contact and Addresses IN THE MODEL? The error message is suggesting that you don't.
0

#6 User is offline   caof2005 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 26-December 11

  Posted 27 December 2011 - 09:23 AM

View PostJulieLerman, on 26 December 2011 - 06:19 PM, said:

Hi, I'm looking at your code and looking at my code file open in visual studio which has no problem compiling. I even commented out my code and copy/pasted yours into the file and still have no problem.

I commented out a few of my using namespace directives but could not get your error.

So...are you sure your model is correct? Did intellisense help you build this query or did you copy paste from a digital copy? I suspect that the message saying that context does not have an Addresses property may be telling you exactly what the problem is.

Julie


Julie
Thanks a lot for you interest and you help, as I indicated to other member of the forum ( WhiskeyRomeoLima ) I guess my model is ok.
I tried to follow the directions you indicated in the book.
I was able to generate the model (Model1.edmx) by using the wizard. I got the Contact, Address and vOfficeAddresses entities correctly.
In the properties windows I have:
Entity Set Name = Contacts for the Contact Entity
Entity Set Name = Addresses for the Address Entity
Entity Set Name = vOfficeAddresses for the vOfficeAddresses Entity


I was able to generate also the UML Class diagram (ClassDiagram1.cd) via right clicking / view class diagram on Model1.Designer.cs in the solution explorer.
In the diagram I got the SampleEntities, Address, Contact and vOfficeAddresses classes

And before this exercise all the other code samples I was able to compile the correctly. I´m using Entity Framework 4.1

Do you have an idea what am I missing?

Regards
Carlos
0

#7 User is offline   JulieLerman 

  • Advanced Member
  • PipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 334
  • Joined: 17-September 08

Posted 27 December 2011 - 09:28 AM

I can only try to restate my last question. In the visual model, is there a relationship between Contact and Addresses? If you are in chapter 4, this question should make sense to you. You should already know to look for a) a line indicating an association between those two entities and b ) navigation properties in the entities. Contact entity should have Addresses. Address should have Contact.
0

#8 User is offline   caof2005 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 26-December 11

Posted 27 December 2011 - 09:42 AM

View PostJulieLerman, on 27 December 2011 - 09:28 AM, said:

I can only try to restate my last question. In the visual model, is there a relationship between Contact and Addresses? If you are in chapter 4, this question should make sense to you. You should already know to look for a) a line indicating an association between those two entities and b ) navigation properties in the entities. Contact entity should have Addresses. Address should have Contact.


Yes Julie,

I have a 1 to * ( many ) relationship between Contact and Address entities in the model.
In the Contact entity I have an Address Navigation property
In the Address entity I have a Contact Navigation property ( besides a ContactId normal property ).

All of this was generated automatically.
0

#9 User is offline   JulieLerman 

  • Advanced Member
  • PipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 334
  • Joined: 17-September 08

Posted 27 December 2011 - 09:53 AM

Not sure what next.

Why not simplify the query to see if it will compile and then add things back in one by one to see *exactly* what breaks it.
0

#10 User is offline   caof2005 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 26-December 11

Posted 27 December 2011 - 09:58 AM

View PostJulieLerman, on 27 December 2011 - 09:28 AM, said:

I can only try to restate my last question. In the visual model, is there a relationship between Contact and Addresses? If you are in chapter 4, this question should make sense to you. You should already know to look for a) a line indicating an association between those two entities and b ) navigation properties in the entities. Contact entity should have Addresses. Address should have Contact.


As a matter of fact, I can inspect via intellisense in the "StreetsCities = from a in c.Addresses" statement that indeed there is not Addresses properties but just an Address property in c.

So if I change "c.Addresses" to "c.Address" both errors disappears automatically.
and if a hover over "c.Address" intellisense show me now that this is really an EntityCollection<Address>.Contact.Address type (No metadata documentation available)

So the question is now how can I get all addresses collection from c?

Thanks
0

#11 User is offline   JulieLerman 

  • Advanced Member
  • PipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 334
  • Joined: 17-September 08

Posted 27 December 2011 - 10:24 AM

c.address (as an EntityCollection of Address types) will give you the addresses even if it was called "Foo". You can change the name of the navigation propert in the designer so taht it's Addresses.

Visual Studio should have automatically made that Addresses for you. So I think you are either using Visual Studio 2008 or the project is marked as a .NET 3.5 project, not a .NET 4.0 project.
0

#12 User is offline   WhiskeyRomeoLima 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 30
  • Joined: 29-October 11

Posted 28 December 2011 - 12:19 PM

I finally tracked down the post in MSDN forums where I had the exact same problem.

System.Data.Entity is the using statement that I was missing. Once I included that namespace, everything worked.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users