lambda 分类抽取N条
从每个分类中,按照比例抽取
lista中percent是比例,按照这个比例抽取。
static List<A> lista = new List<A>()
{
new A(){aid=1,percent=30,cid=1},
new A(){aid=2,percent=30,cid=1},
new A(){aid=3,percent=40,cid=1},
};
static List<B> listb = new List<B>()
{
new B(){bid=1,bname="1t",aid=1},
new B(){bid=2,bname="2t",aid=1},
new B(){bid=3,bname="3t",aid=1},
new B(){bid=4,bname="4t",aid=1},
new B(){bid=5,bname="5t",aid=2},
new B(){bid=6,bname="6t",aid=2},
new B(){bid=7,bname="7t",aid=2},
new B(){bid=8,bname="8t",aid=2},
new B(){bid=9,bname="9t",aid=3},
new B(){bid=10,bname="10t",aid=3},
new B(){bid=11,bname="11t",aid=3},
new B(){bid=12,bname="12t",aid=3},
new B(){bid=13,bname="13t",aid=4},
};
static void Main()
{
var o = listb.Join(lista, b => b.aid, a => a.aid, (b, a) => new { b.bid,b.aid, b.bname, a.percent })
.GroupBy(x => x.aid)
.SelectMany(t => t.OrderBy(x=>Guid.NewGuid()).Select((b, i) => new { b.aid,b.bid,b.bname,b.percent, i }))
.Where(x=>x.i+1<=x.percent/10).ToList();
foreach (var item in o)
{
Console.WriteLine(item.aid+" "+item.bname);
}
Console.ReadLine();
}