Estevao Santiago Estevao Santiago - 4 days ago 7
C# Question

EF - anonymous type cannot have multiples...

I have a method with which is the following:

using (ubmmsEntities db = new ubmmsEntities())
{
var result = (from l in db.log_documents
join t in db.teams on l.op_user_team equals t.id
where l.tracking_id == trackingID
select new { l.op_code, l.op_date, l.op_description, l.op_refer_code, l.op_refer_comments, t.team_name, l.id });
return result.


Now I have to add another column it, and in simple SQL all I would need is to add two more inner joins, but I am quite lost how to do it in EF a the below results a error under t2.team_name "An anonymous type cannot have multiple properties with the same name".

using (ubmmsEntities db = new ubmmsEntities())
{
var result = (from l in db.log_documents
join t in db.teams on l.op_user_team equals t.id
join r in db.refers_codes on l.op_refer_code equals r.code
join t2 in db.teams on r.id equals t2.id
where l.tracking_id == trackingID
select new { l.op_code, l.op_date, l.op_description, l.op_refer_code, l.op_refer_comments, t.team_name, l.id, t2.team_name });
return result.ToList();
}


So, I looked over here and there, and found this thread, but I am unable to figure out how to apply the solution to my method. I googled for "EF naming types" and similar to try to understand what is being asked from me, but honestly I was unable to figure out by myself.

So, I believe the problem is because EF wants me to set a unique name to my db.team... which I thought I'd given it by calling it t2. This approach does work on SQL, but I do not understand how to apply the same to my method. I did find some clues such as
new{t2=t.id}
but different errors starting to pop-up in different places.

Help/Directions please?

Ric Ric
Answer

You can give names to properties in your projectction ie

.Select(x => new { firstItem = x.y, secondItem = x.x });

You can use this to get around your problem.

Or, as you are doing it:

select new { firstItem = x.y, secondItem = x.x } .....

Comments