0

I am new to LINQ lambda expression and i have been stuck for a while regarding the below issue. I want to perform a left outer join and want to select the left table not the right table but the below query gives me error when I select the Left table

The "query" is an IQueryable and also the "model2"

enter image description here

 var model = query.GroupJoin(model2,
                    o => o.plu,
                    m => m.plu,
                    (o, m) => new
                    {
                        SmartCoupon = o,
                        Product = m.DefaultIfEmpty(),
                    })
                    .SelectMany
                    (
                        a => a.SmartCoupon
                    );

Below is the Correct query with the right table but i need the left table

var model = query.GroupJoin(model2,
                    o => o.plu,
                    m => m.plu,
                    (o, m) => new
                    {
                        SmartCoupon = o,
                        Product = m.DefaultIfEmpty(),
                    })
                    .SelectMany
                    (
                        a => a.Product 
                    );
Kinnan Nawaz
  • 399
  • 2
  • 7
  • 24

1 Answers1

5

You are misunderstanding SelectMany().

It is used to flatten List inside another List you need Select() here:

.Select(a => a.SmartCoupon);

Suppose you have List<List<string>>`` and you want all string in aList, then you need to useSelectMany()`.

For Example:

var nameList = new List<List<string>>()
                            {
                                new List<string>
                                    {
                                       "Matt","Adam","John","Peter","Owen"
                                    },
                                new List<string>
                                    { 
                                       "Tim","Jim","Andy","Fred","Todd"
                                    }
                            };

now use SelectMany() to get a single IEnumerable<string>, if you use Select it will return IEnumerable<IEnumerable<string>>:

var namesInSingleList = nameList.SelectMany(x => x);

you can see difference between Select and SelectMany in this SO post

Community
  • 1
  • 1
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • Okay . But If i replace select will I have a left join ? Because I looked into the output sql query in the sql pro filer and there is no left outer join with product table in the output query – Kinnan Nawaz Mar 13 '15 at 11:08
  • 5
    ``Select`` and ``SelectMany`` have nothing to do with left join – Ehsan Sajjad Mar 13 '15 at 11:09
  • okay How about I want to Merge two tables record in the selectmany and return them as one list. like this cross join http://stackoverflow.com/a/13035259/1202610 – Kinnan Nawaz Mar 13 '15 at 11:21
  • Trying to understand. working I am stuck on this for too long now. wait. can we chat on stackoverflow. please – Kinnan Nawaz Mar 13 '15 at 11:34
  • Okay when I do This var model = query.GroupJoin(model2, o => o.plu, m => m.plu, (o, m) => new { SmartCoupon = o, Product = m.DefaultIfEmpty() }); I get the this result in the query "select * from (select * from SmartCoupon left join Product)" which is right but I am unable to get the records of the outer most select statment – Kinnan Nawaz Mar 13 '15 at 11:39