joins in Linq using lists<strings> with no id to index from

Hi and thanks in advance.

I have 2 lists:

    String[] a = new String []{"A","B", "C"}; 
    String[] num = new String []{"1","2" ,"3"};

And a linq query:

    var gridview = (from A in a
                    from num in Num
                    select new
                    {
                        TName = A,
                        RName = Num

                    }).Distinct().ToList();

    GridViewList.DataSource = gridview;
    GridViewList.DataBind();

So this generates output like:

1 A
2 A
3 A
1 B
2 B
3 B
1 C
2 C
3 C

What I want is output like:

1 A
2 B
3 C

So I thought I could use a join but how if I don't have an id to refer to?

Change to:

   var gridview = a.Zip(num, (t, r) => new 
   {
       TName = t,
       RName = r
   }).ToList();

Answers


What you want to do here is Zip the lists

var gridview = a.Zip(num, (A,n)=>new{TName = A,RName = n}).ToList();

See http://msdn.microsoft.com/en-us/library/dd267698(v=vs.110).aspx


You can use Zip:

var result = a.Zip(num, (s, n) => n.ToString() + " " + s);

You need to use Zip not Join, assuming the requirement is to merge two lists of equal length.


You can use following approach:

var res = a.Select((x,i) => new {TName = x, RName = num[i]}).ToList();

Need Your Help

Changed DB, updated DB version can't add data and can't not add data

java android sqlite insert

I am working on an android application that uses two databases. Recently, I had to add a new column to one of the databases. Upon doing so, it broke my database. Installing and re-installing the

Editing all CSS rules relative to one id

css

I have a css and i want to put all the rules in css relative to an id.