Web Technologist
An anonymous type cannot have multiple properties with the same name
If you are using LINQ and have a query that looks something like this :
var myUser = (from user in dbContext.Users
join comp in dbContext.Companies on user.CompanyID equals comp.CompanyID
where user.UserID == "12345"
select new {user.UserID, user.Name, comp.Name}).SingleOrDefault();
string _userID = myUser.UserID;
string _userName = myUser.Name;
string _companyName = myUser.Name;
In the above code, example we are using a syntactic sugar feature of C# 3.0 by building anonymous types. When you try to compile the above code, you will get an “An anonymous type cannot have multiple properties with the same name” error. This is because the compiler can’t differentiate if the Name property belongs to the Users table or the Companies table. So to solve this problem, we need to assign something like an alias to the conflicting properties.
var myUser = (from user in dbContext.Users
join comp in dbContext.Companies on user.CompanyID equals comp.CompanyID
where user.UserID == "12345"
select new {user.UserID, UserName = user.Name, CompanyName = comp.Name}).SingleOrDefault();
string _userID = myUser.UserID;
string _userName = myUser.UserName;
string _companyName = myUser.CompanyName;
Happy coding!!!
| Print article |
Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |

