本文介绍了使用LINQ两个日期之间的重叠记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,用户可以委派其他用户批准的订单。



我需要验证什么,就是没有循环引用,所以如果用户A 被委派用户B 在一段时间内,用户B 可没有委托用户A 该期间内的任何时间,即有一个重叠(日期在 DD / MM / YYYY ):



委托者委托给DateFrom DateTo
用户A用户b 1月11日/ 2012 2012年8月11日
用户A用户b 16/12/2012 24/12/2012

这是我要检查重叠的代码:

  VAR重叠= .Session.QueryOver< D​​elegateOrderApproval>() 
。凡(X => x.DateFrom> DateTime.Now和放大器;&安培; delegateOrderApproval.DelegatedUser.UserId == x.DelegatorUser.UserId)
。而(X => x.DateFrom > = delegateOrderApproval.DateFrom和放大器;&安培; delegateOrderApproval.DateTo< = x.DateTo
|| (x.DateFrom> = delegateOrderApproval.DateFrom和放大器;&安培; x.DateTo< = delegateOrderApproval.DateTo)
|| (x.DateFrom< = delegateOrderApproval.DateFrom和放大器;&安培; x.DateTo> = delegateOrderApproval.DateFrom)
|| (x.DateFrom< = delegateOrderApproval.DateFrom和放大器;&安培; x.DateTo> = delegateOrderApproval.DateTo))
的.List();

如果我尝试添加以下记录:



委托者:用户b
委派为:用户A
日期从:15/11/2012
DateTo: 21/11/2012

应该没有冲突,因为用户b 是不是在该时间段为用户A 的委托?它拿起纪录在12月。



我已经寻找日期范围重叠的代码,并试图一切,但似乎没有任何工作。



任何想法?


解决方案

你能不能简单地做

  VAR doesOverlapExist = Session.QueryOver< D​​elegateOrderApproval>()
。任何(X =>
x.DateFrom> DateTime.Now和放大器;&安培;
delegateOrderApproval.DelegatedUser .UserId == x.DelegatorUser.UserId&放大器;&安培;
x.DateFrom< = delegateOrderApproval.DateTo和放大器;&安培;
x.DateTo> = delegateOrderApproval.DateFrom);



或者如果你没有任何与


$ B代替$ b

 其中(x =>布拉布拉).Count之间的()大于0 

重叠检查逻辑:


In my application a user can delegate another user to approve orders.

What I need to validate, is that there are no circular references so if User A is has delegated User B over a certain period, User B can not delegate User A for any time within that period, i.e there is an overlap (dates in dd/mm/yyyy):

Delegator    Delegated To    DateFrom    DateTo
User A       User B          01/11/2012  08/11/2012
User A       User B          16/12/2012  24/12/2012

This is the code I have to check for overlaps:

var overlaps = .Session.QueryOver<DelegateOrderApproval>()
    .Where(x => x.DateFrom > DateTime.Now && delegateOrderApproval.DelegatedUser.UserId == x.DelegatorUser.UserId)
    .And(x => x.DateFrom >= delegateOrderApproval.DateFrom && delegateOrderApproval.DateTo <= x.DateTo
              || (x.DateFrom >= delegateOrderApproval.DateFrom && x.DateTo <= delegateOrderApproval.DateTo)
              || (x.DateFrom <= delegateOrderApproval.DateFrom && x.DateTo >= delegateOrderApproval.DateFrom)
              || (x.DateFrom <= delegateOrderApproval.DateFrom && x.DateTo >= delegateOrderApproval.DateTo))
    .List();

If I try to add the following record:

Delegator: User B
Delegated  To: User A
Date From: 15/11/2012
DateTo:    21/11/2012

there should be no conflict as User B is not a delegate for User A in that time period? It's picking up the record in December.

I have searched for date range overlapping code and tried everything but nothing seems to work.

Any ideas?

解决方案

Can't you simply do

var doesOverlapExist = Session.QueryOver<DelegateOrderApproval>()
     .Any(x => 
        x.DateFrom > DateTime.Now && 
        delegateOrderApproval.DelegatedUser.UserId == x.DelegatorUser.UserId &&
        x.DateFrom <= delegateOrderApproval.DateTo &&
        x.DateTo >= delegateOrderApproval.DateFrom);

or if you don't have any, replace with

Where(x=> blabla).Count() >0

overlap check logic :Determine Whether Two Date Ranges Overlap

这篇关于使用LINQ两个日期之间的重叠记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 15:27