1

I have simple linq query with join statement results in anonymous type. The problem is two properties have same name. How to get this to work. Suppose I have the following code

Dim retQry = From cb In _entityCtx.CandyBars Join soda in _entityCtx.Drinks On cb.Id Equals soda.Id Select cb.Id, Soda.Id, Soda.Price, cb.Name 

I get the error Range variable Id is already declared.

UPDATE: Found microsoft document that shows only property name is used hence 'Id' is field that gets passed around. In anonymous types from query express https://msdn.microsoft.com/en-us/library/bb384767.aspx

3
  • 2
    Not sure about the syntax, but you can explicitly specify a property name. So use cbId and SodaId, in C# it is like select new { cbId = cb.Id, SodaId = soda.Id.... Commented Jan 23, 2015 at 21:11
  • 1
    Select new { cbid = cd.Id, sodaid= Soda.Id, ... ? Commented Jan 23, 2015 at 21:13
  • Thanks Habib and Giorgos and dotnetom. Commented Jan 23, 2015 at 21:22

1 Answer 1

1

You cannot use 2 properties with the same name, but you can use select and create anonymous type with 2 properties with different names like this:

Select New With {.cbId = cb.Id, .SodaId = Soda.Id, .Price = Soda.Price, .Name = cb.Name} 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.