tags:

views:

39

answers:

3

I have two string arrays ( I will not use it anywhere as it is not a good logic,this is for my learning purpose).

string[] names = new[] { "Joss", "Moss", "Kepler" };

string[] emails = new[] { "Moss@gmail", "Kepler@gmail", "Joss@gmail" };

How to use LINQ so as to make key value pairs like

{"Joss","Joss@gmail"} ,{"Moss","Moss@gmail"} , {"Kepler","Kepler@gmail"}
  1. email ids are shuffled in string array emails[]
  2. consider both string arrays keep unique name and unique email ids (their name as email ids).

How to use Join to project the result i want?

                          var JoinDemo = from grpname in names
                          join
                          grpmails in emails 
                          on grpname ????????
+1  A: 

Sounds like you want the Zip operator from .NET 4.0. If you can't wait that long, you can use the implementation in MoreLINQ.

I'm not sure what you mean about the shuffling though... do you really want a random pairing? If so, it's probably easiest to shuffle one array first and then do the zip.

Jon Skeet
no not random selection. I shuffled emails[].During selection,the search should select the proper match ( undisired match {Joss,[email protected]} desired match { Joss,[email protected]}.Hope it would clear.Willing to provide info if u still need something.
udana
No, it's still not particularly clear. What represents a match? Just that the email address starts with the name?
Jon Skeet
yes exactly ,I am curious to know how can we perfom it using MoreLinq."Bruno conde" provided the answer.I wish to know how can we apply MoreLinq on that.
udana
Why did not you add the MoreLinq tag in StackOverFlow ?
udana
@udana: If you're not matching pair-wise sequentially, you don't need Zip. As for a morelinq tag - tags should be about questions, not answers really.
Jon Skeet
A: 

This is the Zip operation which is not available in .NET 3.5 BCL out of the box.

If you have two arrays (won't work for generic IEnumerable<T>) of same length, you can use:

var zippedArrays = Enumerable.Range(0, Math.Min(a.Length, b.Length))
                             .Select(i => new { Name = a[i], Email = b[i] })
                             .ToList();
Mehrdad Afshari
It is working fine.But each name should match to their associate email (here associate represents their name@gmail) for example if the name is Jossi need Joss@gmail {"Joss","Joss@gmail"} in second string array (emails[]) emailids are shuffled.
udana
+1  A: 

With the constraints you defined this should do the job:

        var q = from name in names
                join email in emails on name equals email.Remove(email.IndexOf('@'))
                select new
                {
                    Name = name,
                    Email = email
                };
bruno conde
Yes Bruno Conde,It is working fine.
udana
Thanks.The scroll bar appears down.somebody please edit it.
udana