Friday 23 June 2017

Ananymous elements have same name in LINQ



This Exception came when  i try to query using LINQ to get the data in a variable as below :
var countryDetails = (from count in Supplier
                      where count.AddressInformation != null && count.AddressInformation.CountryInformation != null
                      select new
                      {
                          count.AddressInformation.CountryInformation.LongName,
                          count.AddressInformation.StateInformation.LongName

                      }).ToList();
To Resolve The Exception, We have to give Names to each variable in new object variable

i.e.,
var countryDetails = (from count in Supplier
                      where count.AddressInformation != null && count.AddressInformation.CountryInformation != null
                      select new
                      {
                          CountryName = count.AddressInformation.CountryInformation.LongName,
                          StateName = count.AddressInformation.StateInformation.LongName

                      }).ToList();

No comments:

Post a Comment

Note: only a member of this blog may post a comment.