我的日期详情格式为(dd / mm / yyyy),
表#1
id Name date User
--------------------------------
1 xxx 01/01/2016 user1
2 yyy 01/02/2016 user2
3 aaa 02/03/2016 user1
表#2
id Name date
--------------------
1 xxx 07/01/2016
2 xxx 09/01/2016
3 yyy 05/02/2016
4. aaa 04/03/2016
编辑1: 逻辑是,第一次在表2中出现xxx意味着,它从Table1获取日期以计算日差。对于下一次出现xxx值意味着,它从Table2获得日期(而不是table1)因为Table2已经具有该表中的值。因此,日差将通过仅使用Table2来计算。其他值也一样。 编辑2: 实际上,我需要获取值的日期差异。如果名字第一次出现,我必须使用Table1来获取日期差异。并且为了从表2获得Dtae差异,如果之前发生了名称。 我将'Name'字段设为UNIQUE 结果应该是,
user Name DateDifference
------------------------------------
user1 xxx 6 (07/01/2016 - 01/01/2016)
user1 xxx 2 (07/01/2016 - 09/01/2016)
user2 yyy 4 (01/02/2016 - 05/02/2016)
user1 aaa 2 (02/03/2016 - 04/03/2016)
我试过的LINQ查询是,
var query = (from item1 in Table1
join item2 in Table1 on item1.Name equals item2.Name
where item2.Date > item1.Date
join origin in Table2 on item1.Name equals origin.Name
where // condition
group new { item1,item2 } by new { item1.Name,item1.Date } into grp
select new
{
DayDiff = DbFunctions.DiffDays(grp.Key.Date, grp.Min(d => d.item2.Date)),
}
).ToList();
但我无法得到预期的结果。我也知道这个问题How to get the date difference by joining two tables?,但最后他们没有在适当的LINQ查询选项中结束它。
我的问题与How to get the date difference by joining two tables?有何不同? 答案是在SQL中。我需要在LINQ中编写代码。
答案 0 :(得分:1)
这是一种可能的方式:
var query =
(from t1 in db.Table1
join t2 in db.Table2 on t1.Name equals t2.Name
let prevDate = (from t3 in db.Table2
where t3.Name == t2.Name && t3.Date < t2.Date
select (DateTime?)t3.Date).Max() ?? t1.Date
select new
{
t1.User,
t1.Name,
DayDiff = DbFunctions.DiffDays(prevDate, t2.Date).Value,
}).ToList();
答案 1 :(得分:1)
我希望至少你有这两个表的导航属性。试试这个
class Program
{
static void Main(string[] args)
{
using (var db = new aaContext2())
{
IList<DTO> dto = new List<DTO>();
dto = db.Table2.Select(a => new DTO
{
Name = a.Name,
User = a.Namea.User,
DateDifference = (DbFunctions.DiffDays(a.Date, db.Table2.Where(aa => aa.Name.Equals(a.Name) && a.Date < aa.Date).Min(dd => dd.Date)
) ?? (DbFunctions.DiffDays(db.Table1.Where(aa => aa.Name.Equals(a.Name)).Min(aaa => aaa.Date), a.Date)))
}).ToList();
}
}
}
public class DTO
{
public string User { get; set; }
public string Name { get; set; }
public int? DateDifference { get; set; }
}
public class aaContext2 : DbContext
{
public DbSet<Table1> Table1 { get; set; }
public DbSet<Table2> Table2 { get; set; }
}
public class Table1
{
public int Id { get; set; }
public string Name { get; set; }
public virtual IList<Table2> NameList { get; set; }
public DateTime Date { get; set; }
public string User { get; set; }
}
public class Table2
{
public int Id { get; set; }
public virtual Table1 Namea { get; set; }
public string Name { get; set; }
public DateTime Date { get; set; }
}
}
答案 2 :(得分:0)
我希望日期差异在条件之内,而不是在查询之外,所以这个表单帮助了我:
上找到它以下是一个示例,展示了从现在到现在14天之间让所有员工获得DOB的方法......
var employeesWithBirthday = from emp in dc.Employees let BirthdayDiff = (new DateTime(DateTime.Now.Year, emp.BirthDate.Month, emp.BirthDate.Day) - DateTime.Now).TotalDays where BirthdayDiff >= 0 && BirthdayDiff <= 14 select emp;
...但是,请注意,像这样的查询会进行表扫描(不能使用任何索引)......