本文介绍了如何在Linq中获得以下输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何模拟此Sql查询:

How to emulate this Sql query:

Select A.FID, A.SLNO, A.Date as FromDate, A1.Date ToDate
From #Test A
Inner Join #Test A1 On A.FID = A1.FID And A1.SLNO = A.SLNO + 1

其中:

使用此查询给出相同的输出:

To give the same output using this query:

var results2 = from table1 in dtSplitDates.AsEnumerable()
join table2 in dtSplitDates.AsEnumerable() on new { FID = table1.Field<int>("FID"), SLNO = table1.Field<int>("SLNO")} equals new { FID = table2.Field<int>("FID"), SLNO = table2.Field<int>("SLNO") }  into lj
from r in lj.DefaultIfEmpty()
select dtSplitDates2.LoadDataRow(new object[]
{
    r["FID"],
    r["SLNO"],
    r == null ? string.Empty : r["Dates"]
}, false);

我无法修改我的选择列表和加入条件 - 当我尝试我得到对象引用未设置为对象的实例。有2个表 dtSplitDates dtSplitDates2

I am unable to modify my select list and join condition - when i try i get Object reference not set to an instance of an object. There are 2 tables dtSplitDates and dtSplitDates2

这就是表 dtSplitDates 现在看起来, dtSplitDates2 是它的克隆:

This is how the table dtSplitDates looks now and dtSplitDates2 is its clone:

推荐答案

什么是dtSplitDates2?

What is dtSplitDates2 ?

分享你的表格内容。

但是我明白了,我宁愿这个解决方案

But what I understood, I would prefer this solution

     var results2 = from table1 in dt1.AsEnumerable()
                       join table2 in dt2.AsEnumerable() on new { FID = table1.Field<int>("FID"), SLNO = table1.Field<int>("SLNO") } equals new { FID = table2.Field<int>("FID"), SLNO = table2.Field<int>("SLNO") } into lj
                       from r in lj.DefaultIfEmpty()
                       select new
                       {
                           FID = r["FID"],
                           SLNO = r["SLNO"],
                           Dates = r == null ? string.Empty : r["Date"]
                       };

这篇关于如何在Linq中获得以下输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 10:19