如何在LINQ take()方法中使用条件

时间:2017-04-11 12:55:42

标签: linq

如何连接两个表并根据B类变量值获取记录

Class A
{
    RecordID int {get;set;}

    Name string {get;set;}
}

Class B
{
    Name string {get;set;}

    DataToPick int  {get;set;}
}

我的输入是

Class A
---------------------
RecordID name

1        Sathish

2        Sathish

3        Kumar

4        Kumar

5        Ajay

Class B
--------------------------------------
Name   DataToPick

Sathish   1

kumar     2

现在我想加入两个表并根据B类变量(Datatopick)获取A类中的值,即3条记录

请帮助在LINQ中编写,如下所示

var qry= (from c in classA
          join d in ClassB on c.name equals d.name
         select c).take(d.datatopick)

2 个答案:

答案 0 :(得分:1)

您可以对ClassA进行分组,然后从其他列表中选择DataToPick

var result = 
     classA.GroupBy(a => a.Name)
           .SelectMany(g => g.Take(classB.Where(b => b.Name == g.Key)
                                         .Select(b => b.DataToPick)
                                         .DefaultIfEmpty(0)
                                         .Single()));

更好地加入第一个和拍摄:

var result = classA.Join(classB, a => a.Name, b => b.Name, (a, b) => new {a, b.DataToPick})
                   .GroupBy(arg => new {arg.a.Name, arg.DataToPick})
                   .SelectMany(g => g.Select(arg => arg.a).Take(g.Key.DataToPick));

答案 1 :(得分:1)

您可以bot.dialog('A', [ function (session, args, next) { ... }, function (session, args, next) { ... }, function (session, args) { ... } ]).triggerAction({ matches: 'A' }); bot.dialog('B', [ function (session, args, next) { ... }, function (session, args, next) { ... }, function (session, args) { ... } ]).triggerAction({ matches: 'B' }); classA和classB,按join分组,然后使用Name, DataToPick

Take