我似乎在最内层for循环的查询中丢失了intellisense,这让我相信我写的是错误的代码。这似乎是匹配声明的问题。当我尝试通过简单捕获值来匹配时,一切似乎都有效。当我尝试通过比较值进行匹配时,代码似乎打破了。这是代码:
let createOrderFromStdOrder () =
// retrieve standard orders
let stdOrders =
Queries.retrieveStdOrders null |> Seq.toList
// retrieve orders
let orders = Queries.retrieveOrders null |> Seq.toList
// current date
let curDate = DateTime.Now
// iterate and create order
for i in stdOrders do
let validDates = MyUtil.stdDeadlineCheck i
// process standard order
let orderDate =
if not validDates.IsEmpty then
let daysOfWeek =
query{
for row in validDates do
sortBy row
take 2
}
for j in daysOfWeek do
match j with
| c -> // this seems to work
let order =
query{
for o in orders do
where(o.Works = i.Works) // and I get dot intellisense o and i
}
但如果我尝试匹配这样似乎打破了:
for j in daysOfWeek do
match j with
| DateTime.Now.DayOfWeek -> // this does not work
let order =
query{
for o in orders do
where(o.NOTHING = i.Works) // NO dot intellisense on o
}
我怀疑这可能与嵌套有关。我怎么能纠正这个?
答案 0 :(得分:2)
模式匹配只能测试与常量的相等性,而不能测试DateTime.Now.DayOfWeek
等值。原因是当值是一个简单的变量名时会使它变得模糊,因为模式中的变量用于将此位置的任何值绑定到名称,而不是将其与现有变量进行比较:
match j with
| x ->
// in here, x is equal to j.
在您的具体情况下,您可以使用if
:
for j in daysOfWeek do
if j = DateTime.Now.DayOfWeek then
let order = // ...
但是,如果在更复杂的情况下,您希望与内部模式进行比较,则可以使用guard:
match someRecord with
| { someField = d } when d = DateTime.Now.DayOfWeek ->
let order = // ...