Take the 2-minute tour ×

I got the error

cannot implicity convert type 'System.Linq.IQueryable<EntityNetimoveis.San_Imovel>' to 'EntityNetimoveis.San_Imovel' An Explicit conversion exists (are you missing a cast ?)

This occurs when I try the following code

EntityNetimoveis.San2011Entities db = new EntityNetimoveis.San2011Entities();
EntityNetimoveis.San_Imovel im = db.San_Imovel.Where(a => a.Credenciada_Id == 10);

What type of conversion I have to do ?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

The type of im is an IQueryable<EntityNetimoveis.San_Imovel> because you are running a where query and there might be more than one result. If you want the first result matching your query you should use .FirstOrDefault(). Try changing you code as:

EntityNetimoveis.San_Imovel im = db.San_Imovel.FirstOrDefault(a => a.Credenciada_Id == 10);

If you want to return more than one entity and process them, lets say using a foreach loop, then you need to change the type of the return value to IEnumerable<Netimoveis.San_Imovel>. See the following example:

IEnumerable<EntityNetimoveis.San_Imovel> ims = db.San_Imovel.Where(a => a.Credenciada_Id == 10);

The you can use the following:

foreach(var im in ims) {
    // your code goes here
share|improve this answer
But I need more than one result, can be millions of registers that has a.Credenciada_Id == 10. I need to save this in Entity.San_Imovel and then I need to do a ForEach. How can I perform this ? –  Lucas_Santos Aug 24 '12 at 13:57
Updated my answer –  Mennan Kara Aug 24 '12 at 14:05
add comment

If the lambda can return more than one record:

var imList = db.San_Imovel.Where(a => a.Credenciada_Id == 10).ToList();
share|improve this answer
I have to return all register in my database that has the column Credenciada_Id equals 10. Can be millions of registers, so I need to put in San_Imovel all these registers, and then I'll do a Foreach –  Lucas_Santos Aug 24 '12 at 13:55
Is SingleOrDefault supported in Linq-To-Enitities? –  Tim Schmelter Aug 24 '12 at 13:55
I have edited my answer –  Massimiliano Peluso Aug 24 '12 at 13:57
Exists, but I need to return more then one result. –  Lucas_Santos Aug 24 '12 at 13:58
@Lucas_Santos The expression Massimiliano is using will return a List<SanIMovel>. Theoretically, you could leave off the .ToList part and you'd have a IQueryable<SanIMovel>, which implements IEnumerable<SanIMovel>. Both methods will give you more than one result if more than one result exist. –  villecoder Aug 24 '12 at 14:00
add comment

Your Answer


