如何在datatable中的两列之间获取数据? [英] How to get data between two columns in datatable?

查看:360
本文介绍了如何在datatable中的两列之间获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有两个数据表:

1-penaltyrule

  ser  from-min  to-min  pen
   1     1         55     1
   2     56        90     2
   3     91        null   3

2- penaltyEmp

   ser  emp  tot-min 
    1   782   2
    2   672   67
    3   677   92
    4   56    7






我想用LINQ获取每个用户的笔


I want to get the pen for each user with LINQ

我的意思是 tot-min BETWEEN from-min AND to-min SELECT
pen

i mean where tot-min BETWEEN from-min AND to-min SELECT pen

我想要具有以下结果的数据表:

I want data table with the following result :

emp   pen
782   1
672   2
677   3
56    1


推荐答案

您可以使用此查询:

var penaltyEmps = penaltyEmp.AsEnumerable()
    .Select(r => new { ser = r.Field<int>("ser"), emp=r.Field<int>("emp"), tot_min=r.Field<int>("tot-min"), row = r });
var penaltyrules = penaltyrule.AsEnumerable()
    .Select(r => new { ser = r.Field<int>("ser"), from_min=r.Field<int>("from-min"), to_min=r.Field<int>("to-min"), row = r });

DataTable tblResult = penaltyEmps
    .Select(x => new
    {
        penaltyEmp = x,
        matchingRules = penaltyrules.Where(x2 => x.tot_min >= x2.from_min && x.tot_min <= x2.to_min)
    })
    .Where(x => x.matchingRules.Any())
    .Select(x => x.penaltyEmp.row)
    .CopyToDataTable();

这篇关于如何在datatable中的两列之间获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆