0

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

Community
  • 1
  • 1
Tom Cruise
  • 1,395
  • 11
  • 30
  • 58
  • 2
    It looks complicated because you're thinking about the presentation without thinking about your expected object model. Focus on the latter first, and the former will easily follow. What's a logical way to represent your missing records in memory? Then focus on getting your query to return that. Once you have that working, focus on getting your missing records to display as "none". Unfortunately, you've left out so many relevant details in this question that I don't see a way of giving an even remotely concrete answer. –  Nov 29 '15 at 18:47
  • @hvd . I edited my post. I would like to know how the selectmany syntax works here? and on what basis the mapping has been done? – Tom Cruise Nov 29 '15 at 19:01
  • Okay, with your edit, I see there are some fundamentals you're not completely getting just yet, mostly lambda expressions. Might I recommend looking for a good tutorial on those, and revisiting your question here afterwards? They're not a difficult concept, but can be tricky to get your head around in the beginning, and I get the impression that after understanding lambda expressions, there's still more in your question that needs answering that's best separated from an explanation of C# language features. –  Nov 29 '15 at 20:35

1 Answers1

0

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);
            }

        }
Ziv Weissman
  • 4,400
  • 3
  • 28
  • 61