简体   繁体   中英

group join -left outer join- The type arguments for method 'System.Linq.Enumerable.DefaultIfEmpty cannot be inferred from the usage. Error

I have the following snippet in which i tried to use Left join. If no records found i want to display it as "none".

  var customers = new Customer[]
    {
        new Customer{Code = 5, Name = "Sam"},
        new Customer{Code = 6, Name = "Dave"},
        new Customer{Code = 7, Name = "Julia"},
        new Customer{Code = 8, Name = "Sue"}
    };

    // Example orders.
    var orders = new Order[]
    {
        new Order{KeyCode = 5, Product = "Book"},
        new Order{KeyCode = 6, Product = "Game"},
        new Order{KeyCode = 7, Product = "Computer"},
        new Order{KeyCode = 7, Product = "Mouse"},
        new Order{KeyCode = 8, Product = "Shirt"},
        new Order{KeyCode = 5, Product = "Underwear"}
    };

 var source = customers.GroupJoin(
    orders,
    p => p.Code,
    c => c.KeyCode,
    (p, g) => g
        .Select(c => new { PID = p.Code, CID = c.KeyCode })
        .DefaultIfEmpty(new { PID = 0, CID }))//Error   1   The type arguments for method 'System.Linq.Enumerable.DefaultIfEmpty<TSource>(System.Collections.Generic.IEnumerable<TSource>, TSource)' cannot be inferred from the usage. Try specifying the type arguments explicitly.   

    .SelectMany(g => g);
    // Enumerate results.

I understood how the group join can be applied. But If i want to display left outer join records by modifying the previous query, what changes needs to be done?

Someone please help me to understand this. how the assignment and variable declaration happening here?

Edit: I modified as i found another post but getting error in the line defaultifempty. Do i need to modify anything to make this work?

left outer join in lambda/method syntax in Linq

I am not sure in your example what is X,Y but the answer to what you seek is much simpler.

This will join your 2 tables, and with the func (order,customer) you can select which parameters you what in your result, or select the entire object:

       var query = orders.Join         //Join source table
            (customers,                 //Outer table
            m => m.KeyCode,             //The foreign key from source to outer table
            o => o.Code,                //The 'primary' key of target table
            (order, customer) =>       //func for parameters

            new { order, customer }).GroupBy(m=>m.customer.Code); //Your Result view

            //In sql this is something like:
        /*  SELECT left.Product, 
         *         right.Name 
         *         from Orders as 'left'
         *         left join customers as 'right' on 
         *         left.KeyCode == right.Code
         */

        foreach (var outerItem in query)
        {
            Debug.WriteLine("{0} bought...", outerItem.FirstOrDefault().customer.Name);
            foreach (var item in outerItem)
            {
                Debug.WriteLine("Product {0}", item.order.Product);
            }

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM