出于这个问题的目的,让我们假设用户来自美国,并将使用标准公历.因此,日历周从星期日开始,到星期六结束.
我要做的是确定两个日期之间存在的日历周数.我的问题的一个完美的例子存在于2010年10月.在10/16和10/31之间有4个日历周.
我宁愿远离任何硬编码逻辑,如:
if (Day == DayOfWeek.Saturday && LastDayOfMonth == 31) { ... }
Run Code Online (Sandbox Code Playgroud)
谁能想到一个合乎逻辑的方法来做到这一点?
更新:
感谢所有伟大的回应,经过一些考虑后,我使用的解决方案:
//get the start and end dates of the current pay period
DateTime currentPeriodStart = SelectedPeriod.Model.PeriodStart;
DateTime currentPeriodEnd = SelectedPeriod.Model.PeriodEnd;
//get the first sunday & last saturday span that encapsulates the current pay period
DateTime firstSunday = DayExtensions.SundayBeforePeriodStart(currentPeriodStart);
DateTime lastSaturday = DayExtensions.SaturdayAfterPeriodEnd(currentPeriodEnd);
//get the number of calendar weeks in the span
int numberOfCalendarWeeks = DayExtensions.CalendarWeeks(firstSunday, lastSaturday);
Run Code Online (Sandbox Code Playgroud)
以下是辅助类的方法:
/// …
Run Code Online (Sandbox Code Playgroud) 想象一下这样的表:
CREATE TABLE [dbo].[test](
[id] [uniqueidentifier] NULL,
[name] [varchar](50) NULL
)
GO
ALTER TABLE [dbo].[test] ADD CONSTRAINT [DF_test_id] DEFAULT (newsequentialid()) FOR [id]
GO
Run Code Online (Sandbox Code Playgroud)
使用如下所示的INSERT
存储过程:
CREATE PROCEDURE [Insert_test]
@name as varchar(50),
@id as uniqueidentifier OUTPUT
AS
BEGIN
INSERT INTO test(
name
)
VALUES(
@name
)
END
Run Code Online (Sandbox Code Playgroud)
获取刚刚插入的GUID并将其作为输出参数返回的最佳方法是什么?
假设我有一个EF实体名称的清单,例如:
List<string> entityNames = new List<string>(){
"Table1",
"Table2",
"Table3"
};
Run Code Online (Sandbox Code Playgroud)
从这个实体列表中,我想分别查询每个实体,类似于:
var result = efContext.Table1.Where(t => ...);
Run Code Online (Sandbox Code Playgroud)
使用反射或黑魔法,我将如何获得对实际实体的引用,以便完成以下工作:
foreach(var e in entityNames)
{
var entity = efcontext.GetType().GetProperties().Where(t => t.Name == e).Single();
var result = efContext.entity.Where(t => ...);
}
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?