这样的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);
}
}
}