-1

I want to know how to compare two IEnumerable collection values using Lambda Expressions.

I already posted this for getting by LINQ but it should acheive by Lambda Expression.

My LINQ code for achieve this is:

var result = from ap in AvailablePacks 
             join rp in RecommendedPacks
             on ap.PackID equals rp.PackID
             select new 
             {  
                  PackQuantity =ap.Quantity
             }; 

Now i want to achieve this by using Lambda Expression. How can I do this?

Omar
  • 16,329
  • 10
  • 48
  • 66
SuryaKavitha
  • 493
  • 5
  • 16
  • 36

1 Answers1

4

So what you're really asking for is a translation from a query expression into lambda expression form? In this case you could use:

var result = AvailablePacks.Join(RecommendedPacks,
                                 ap => ap.PackID,
                                 rp => rp.PackID,
                                 (ap, rp) => new { PackQuantity = ap.Quantity });

See Part 41 of my Edulinq blog series for more information about query expression translations.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thank You Jon... Its really useful for me... This is what i needed i'll check this in my code and let you know – SuryaKavitha Apr 25 '12 at 08:50
  • @SuryaKavitha: Why can't you use the query expression form? I usually would for joins... – Jon Skeet Apr 25 '12 at 08:51
  • Can we use this without join? I dont know about query expression form? can you tell me how to proceed with query expression form? – SuryaKavitha Apr 25 '12 at 09:35
  • 1
    @SuryaKavitha: What you posted in the question *is* a query expression form. You call it "LINQ code" but both versions are using LINQ - it's just different ways of expressing code. Why would you want to express this *without* using a join though? – Jon Skeet Apr 25 '12 at 09:50
  • Fine Jon. I got it. Thanks for your information. My boss asked me why did you use join, cant we use without join to do this? thats y i asked you – SuryaKavitha Apr 25 '12 at 10:00
  • @SuryaKavitha: It's a bit like wanting to iterate over everything without using a loop. Yes, you can if you really want to, via weird approaches - but you shouldn't. You're *naturally* joining two collections, so it makes sense to use a join. – Jon Skeet Apr 25 '12 at 10:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10481/discussion-between-suryakavitha-and-jon-skeet) – SuryaKavitha Apr 25 '12 at 10:27
  • @SuryaKavitha: Sorry, chat doesn't really work with my way of using Stack Overflow. I need to just dip in and out. I'd strongly advise you to read a lot of LINQ tutorials etc. (My Edulinq blog series may well help you, particularly for LINQ to Objects.) – Jon Skeet Apr 25 '12 at 10:28