Monday, September 15, 2014

The type of one of the expression in the join clause is incorrect.Type inference failed in the call to GroupJoin (LINQ to XML)

Hi,
I just tested on grouping two collections using GroupJoin approach in LINQ to XML. In the example, the two collections are Customers and Companies. In which, companies served as grouping for the customers. In simple terms, identify a customer of which company he or she belongs. The code below produced an error The type of one of the expression in the join clause is incorrect.Type inference failed in the call to GroupJoin.
Code:
XElement companiesAndCustomers = new XElement("CompaniesAndCustomers",
   from company in companies
   join customer in customers on 
        company equals customer.CompanyName
        into groupCompany
        select new XElement("Company",
               new XAttribute("CompanyName", company.CompanyName),
               new XAttribute("Country", company.Country),
                   from subCustomer in groupCompany
                        select new XElement("Customer", subCustomer.LastName + ", " + subCustomer.FirstName
                               , new XAttribute("Age",subCustomer.Age))));
As I glanced through the code, I joined an entire company object to a customer CompanyName property. To fix that, I added a CompanyName property to company object that is of exact match with customer's CompanyName type.
Code:
XElement companiesAndCustomers = new XElement("CompaniesAndCustomers",
   from company in companies
   join customer in customers on 
        company.CompanyName equals customer.CompanyName
        into groupCompany
        select new XElement("Company",
               new XAttribute("CompanyName", company.CompanyName),
               new XAttribute("Country", company.Country),
                   from subCustomer in groupCompany
                        select new XElement("Customer", subCustomer.LastName + ", " + subCustomer.FirstName
                               , new XAttribute("Age",subCustomer.Age))));

:)

0 comments:

Post a Comment