一直试图对此进行排序,而且大多数似乎指向使用SingleOrDefault
,我不是,但仍然会收到以下错误:
System.Web.HttpUnhandledException (0x80004005)
: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.InvalidOperationException: Sequence contains more than one element at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
at System.Data.Linq.Table`1.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
at System.Linq.Queryable.Any[TSource](IQueryable`1 source, Expression`1 predicate)
at DOAwayFromDesk.GetNextRecord(Int32 user)
这是代码
public AwayFromDesk GetNextRecord(int user) {
using (Intranet2012DataContext db = DataHelper.GetDataContext()) {
if (!db.AwayFromDesks.Any(w => w.ownerlinkid == user && w.enddate >= DateTime.Now && w.complete == false))
return null;
return (from a in db.AwayFromDesks
where a.ownerlinkid == user
&& a.enddate >= DateTime.Now && a.complete == false
orderby a.startdate , a.enddate
select a).FirstOrDefault();
}
}
不确定还要检查什么。
检查了SQL运行的内容,并且每次只返回一条记录。
exec sp_executesql N'SELECT
(CASE
WHEN EXISTS(
SELECT NULL AS [EMPTY]
FROM [dbo].[AwayFromDesk] AS [t0]
WHERE ([t0].[ownerlinkid] = @p0) AND ([t0].[enddate] >= @p1) AND (NOT ([t0].[complete] = 1))
) THEN 1
ELSE 0
END) AS [value]',N'@p0 int,@p1 datetime',@p0=1,@p1='2014-01-24 08:30:28:877'
exec sp_executesql N'SELECT TOP (1) [t0].[id], [t0].[added], [t0].[ownerlinkid], [t0].[startdate], [t0].[enddate], [t0].[reason], [t0].[contact], [t0].[internalmeeting], [t0].[reminder], [t0].[remindertime], [t0].[recur], [t0].[complete]
FROM [dbo].[AwayFromDesk] AS [t0]
WHERE ([t0].[ownerlinkid] = @p0) AND ([t0].[enddate] >= @p1) AND (NOT ([t0].[complete] = 1))
ORDER BY [t0].[startdate], [t0].[enddate]',N'@p0 int,@p1 datetime',@p0=1,@p1='2014-01-24 08:30:28:890'
我注意到并且有点混乱的是Any正在做这件事
if (!db.AwayFromDesks.Any(w => w.ownerlinkid == user && w.enddate >= DateTime.Now && w.complete == false))
return null;
但SQL就是这个
exec sp_executesql N'SELECT
(CASE
WHEN EXISTS(
SELECT NULL AS [EMPTY]
FROM [dbo].[AwayFromDesk] AS [t0]
WHERE ([t0].[ownerlinkid] = @p0) AND ([t0].[internalmeeting] = 1) AND (NOT ([t0].[complete] = 1))
) THEN 1
ELSE 0
END) AS [value]',N'@p0 int',@p0=1
我没有提到我使用的结束日期,但使用的是我在Linq声明中没有使用的internalMeeting,很奇怪。
10 / Feb / 2014 ::现在尝试将.Any更改为.Count,但仍然会随机获取错误。
if (db.vwAwayFromDeskAnyChecks.Count(w => w.ownerlinkid == user && w.enddate >= DateTime.Now)==0) return null;
这甚至是查询:
SELECT COUNT(*) AS [value]
FROM [vwAwayFromDeskAnyCheck] AS [t0]
WHERE ([t0].[ownerlinkid] = @p0) AND ([t0].[enddate] >= @p1)
难倒?不知道这是怎么可能的。