continuation of the issue How get array in linq to entity?
but now is not array => Dictionary
City type is Dictionary
var sites = (from country in db.Countries
select new
{
Country = country.Title,
Cities = country.Cities.Select(m => m.Title)
})
.AsEnumerable()
.Select(country => new SitiesViewByUser()
{
Country = country.Country,
City = country.Cities.ToArray()
});
update:
public class SitiesViewByUser
{
public string Country { get; set; }
public Dictionary<int, string> City { get; set; }
}
You can use ToDictionary to create a dictionary from a LINQ sequence.
The first part is the key, and the second the value, E.g.
This assumes that
CityonSitiesViewByUseris defined asDictionary<string, string>Your LINQ is quite confusing though. You are creating an anonymous type, asssumed to shape a
Country, but which has aCountryproperty on it, which is infact theTitleof the country (is that the name of the country?).You also have a collection of just the city
Titles, so I’m not sure what value you are going to use in yourCitydictionary on yourSitiesViewByUsertype.Also, what is a Sitie? :\
Update
You could do something like this:
Alternatively, why don’t you change the
SitiesViewByUsertype to be:Then you can do (using fluent syntax):