Sunday, October 18, 2009

An anonymous type cannot have multiple properties with the same name

If the query is like this

adventureworksEntities dbEntities = new adventureworksEntities();
            var query = from PC in dbEntities.productcategory
                        join PSC in dbEntities.productsubcategory
                      on PC.ProductCategoryID equals PSC.ProductSubcategoryID
                        select new { PC.Name, PC.ModifiedDate, PSC.Name, PSC.ModifiedDate };

The compiler will throw the error,”An anonymous type cannot have multiple properties with the same name” .Because the compiler can’t able to find a proper Name.So we want to modify the query with alias name ,some thing like this.



  var query = from PC in dbEntities.productcategory
                        join PSC in dbEntities.productsubcategory
                      on PC.ProductCategoryID equals PSC.ProductSubcategoryID
                        select new { PCName1 = PC.Name, PCModifiedDate1 = PC.ModifiedDate, PSCName1 = PSC.Name, PSCModifiedDate1 = PSC.ModifiedDate };

0 comments:

 
Counter