首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

这样的linq如何写

2013-09-05 
这样的linq怎么写table1a b c d e f g htable21 2 3 4输出a, 1b, 2c, 3d, 4e, 1f, 2g, 3h, 4linq[解决办法

这样的linq怎么写
table1
a b c d e f g h
table2
1 2 3 4
输出
a, 1
b, 2
c, 3
d, 4
e, 1
f, 2
g, 3
h, 4 linq
[解决办法]
参考


            string[] arr1 = new string[] { "a", "b", "c", "d", "e", "f", "g", "h" };
            string[] arr2 = new string[] { "1", "2", "3", "4"};
            var v = arr1.Select((x, i) => x + arr2[i % arr2.Length]);
            foreach(string s in v)
            {
                MessageBox.Show(s);
            }

[解决办法]
void Main()
{
var list1=new string[]{"a", "b", "c", "d", "e", "f", "g", "h"};
var list2=new int[]{1, 2 ,3 ,4};
var query=from l1 in  list1.Select((x,y)=>new {x,y})
          join l2 in  list2.Select((x,y)=>new {x,y})
  on l1.y%list2.Count() equals l2.y%list2.Count()
  select l1.x +","+l2.x;
query.ToList().ForEach(q=>Console.WriteLine(q));

/*
a,1
b,2
c,3
d,4
e,1
f,2
g,3
h,4
*/
}

[解决办法]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1


{
    class Program
    {
        static void Main(string[] args)
        {
            var list1 = new string[] { "a", "b", "c", "d", "e", "f", "g", "h" };
            var list2 = new int[] { 1, 2, 3, 4 };
            if (list2.Count() == 0) throw new ArgumentException();
            var query = list1.Zip(Enumerable.Repeat(list2, list1.Length / list2.Length + 1).SelectMany(x => x), (x, y) => new { x, y });
            foreach (var item in query)
                Console.WriteLine(item.x + ", " + item.y);
        }
    }
}



a, 1
b, 2
c, 3
d, 4
e, 1
f, 2
g, 3
h, 4
Press any key to continue . . .

热点排行