[英]F sharp adding lists
how do you convert an obj list to int type. 如何将obj列表转换为int类型。 I am trying to add two lists using a map function below but it doesn't work on obj lists.
我正在尝试使用下面的map函数添加两个列表,但是在obj列表上不起作用。
let query f=
seq{
let cmd = new OleDbCommand( "SELECT * FROM F" );
let conn = new OleDbConnection( @"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=D:\Users\df\Documents\Vfolio.accdb;
Persist Security Info=False;" )
conn.Open()
let DAdapt = new OleDbDataAdapter("SELECT * FROM F",conn)
let DTab = new DataSet()
let i= DAdapt.Fill(DTab)
let rowCol = DTab.Tables.[0].Rows
let rowCount = rowCol.Count
for i in 0 .. (rowCount - 1) do
yield f (rowCol.[i])
}
let u= query(fun row -> row.[0])
let a= List.ofSeq u
let v=query(fun row -> row.[1])
let b= List.ofSeq v
let c = List.map2 (fun x y-> x + y) a b
error msg: The type 'obj' does not support the operator '+' 错误消息:类型'obj'不支持运算符'+'
Because row.[i]
returns type obj
, your u
and v
become seq<obj>
, and thus your a
and b
become type List<obj>
, and therefore x
and y
are inferred to have type obj
, and of course, you can't add two obj
s, which is exactly what the compiler tells you. 因为
row.[i]
返回的是obj
类型,所以您的u
和v
变为seq<obj>
,因此您的a
和b
变为List<obj>
类型,因此可以推断x
和y
的类型为obj
,当然,您不能添加两个obj
,这正是编译器告诉您的。
If you are sure that row.[0]
如果您确定该
row.[0]
and row.[1]
row.[1]
are numbers of some kind, you should apply the appropriate cast, for example: 是某种数字,则应应用适当的强制转换,例如:
let u= query(fun row -> row.[0] :?> int)
let a= List.ofSeq u
let v=query(fun row -> row.[1] :?> int)
let b= List.ofSeq v
let c = List.map2 (fun x y-> x + y) a b
You can apply this cast in other places, too, depending on your taste and requirements, for example: 您也可以根据自己的口味和要求在其他地方应用此演员表,例如:
let c = List.map2 (fun x y-> (x :?> int) + (y :?> int)) a b
Or: 要么:
let a= u |> Seq.cast<int> |> List.ofSeq
let b= v |> Seq.cast<int> |> List.ofSeq
But I like the first example best, because it applies the cast at the earliest known point and results in the least amount of extra code. 但是我最喜欢第一个示例,因为它在最早的已知点应用了强制类型转换,并导致最少的额外代码量。
But beware: if row.[0]
但要注意:如果
row.[0]
turns out to be not an int
at runtime, you will get an InvalidCastException
. 在运行时结果不是
int
,您将收到InvalidCastException
。
PS In your List.map2
call, you could specify (+)
directly instead of wrapping it in an extra lambda: PS在您的
List.map2
调用中,您可以直接指定(+)
而不是将其包装在一个额外的lambda中:
List.map2 (+) a b
PPS Also, it seems that your List.ofSeq
calls are wasteful, for Seq
also has a map2
: PPS另外,您的
List.ofSeq
调用似乎很浪费,因为Seq
也有map2
:
let u = query(fun row -> row.[0] :?> int)
let v = query(fun row -> row.[1] :?> int)
let c = Seq.map2 (+) u v |> List.ofSeq
PPPS Also, have you noticed that each of the two calls to query
generates its own DB connection, command, adapter, and dataset? PPPS另外,您是否注意到
query
的两个调用中的每一个都会生成自己的数据库连接,命令,适配器和数据集? Did you intend this or did you mean to only have one connection and then fetch different columns from the result? 您是打算这样做还是要仅具有一个连接,然后从结果中获取不同的列? If so, you should only call
query
once: 如果是这样,则只应调用一次
query
:
let c = query( fun row -> (row.[0] :?> int) + (row.[1] :?> int) ) |> List.ofSeq
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.