0

How can I do a Join with another Entity ?

I have this one,

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

I want a JOIN with San_Imovel_Caracteristica. Primary Key and Foreign Key called Imovel_Id

I try this one

IEnumerable<EntityNetimoveis.San_Imovel> im = db.San_Imovel.Join.(IEnumerable<EntityNetimoveis.San_Imovel_Caracteristica>, i => imovel_id, a => imovel_Id).Where(a => a.Credenciada_Id.Equals(10));

but this is a wrong code. Has syntax error.

Lucas_Santos
  • 4,638
  • 17
  • 71
  • 118

3 Answers3

2

Basically, joining is like this.

var im = 
db.San_Imovel.Join(db.San_Imovel_Caracteristica, i => i.imovel_id, a => a.imovel_Id, (i, a) => a)
.Where(a => a.Credenciada_Id.Equals(10));

Edited:

For example,

var result = db.ATable
.Where(a => a.Name == 'test')
.Join(db.BTable, a => a.Id, b => b.Id, (a, b) => a);
Win
  • 61,100
  • 13
  • 102
  • 181
  • I think we are closest of a solution. But I got a error in this solution that you develop. `The type arguments for method 'System.Linq.Enumerable.Join<> cannot be inferred from the usage. Try specifying the type arguments explicity.` – Lucas_Santos Aug 24 '12 at 18:25
  • What is this `(i, a) => a` ? – Lucas_Santos Aug 24 '12 at 18:27
  • Are you comparing same types? For example, both i.imovel_id and a.imovel_id must be interger values. – Win Aug 24 '12 at 18:28
  • (i, a) => a means select San_Imovel_Caracteristica table. If you change it to (i, a) => i, it will return San_Imovel table. – Win Aug 24 '12 at 18:29
  • I added the example, so you can get the clear idea about the syntax. http://stackoverflow.com/a/5038416/296861 – Win Aug 24 '12 at 18:40
0

There's a "." after Join that shouldn't be there

Gonzalo
  • 45
  • 5
0
var query = from EntityNetimoveis.San_Imovel i in db.San_Imovel
    join EntityNetimoveis.San_Imovel_Caracteristica c in db.San_Imovel_Caracteristica on i.imovel_id equals c.imovel_Id
    select i;

return query.ToList();
GrandMasterFlush
  • 6,269
  • 19
  • 81
  • 104
Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90