c# - Linq从对象列表中选择属性并使用速记条件修改它们

时间:2017-04-19 14:50:52

标签: c# linq

我正在探索使用Oracle过程进行一些数组绑定,并且需要将我的对象列表拆分为每个属性的数组。

我这样做:

List<ReviewReasons> reasons; // actually comes from method call

cmd.Parameters.Add(new OracleParameter("checked_flag", OracleDbType.Int32, ParameterDirection.Input))

cmd.Parameters["checked_flag"].Value = reasons.Select(x => x.CheckedFlag).ToArray();

cmd.ExecuteNonQuery();

我遇到的问题是reasons.CheckedFlag值是bool,但数据库期望为0或1.在开始使用数组绑定之前,我在{{{ 1}}像这样:

foreach

我想知道我是否能够以某种方式判断我的选择lambda中的foreach(reason in reasons) { string checkedFlag = reason.CheckedFlag ? "1" : "0"; // then push into an array } 条件。我对Linq真的很陌生,所以非常感谢任何帮助。

1 个答案:

答案 0 :(得分:6)

$newcolumnobj =  [ordered]@{}
#input data into a hash table so that we can more easily reference the `.values` as an object to be inserted in the CSV
$newcolumnobj.add("volume name", $currenttime)

#enumerate $deltas [this will be the object that contains the volume information `$volumedeltas`)
#  add just the new deltas to the newcolumn object
foreach ($item in $deltas){ 
  $newcolumnobj.add($item.volume,$item.delta)
}

$originalcsv = @(import-csv $targetdeltacsv)

#thanks to pscookiemonster in #powershell on freenode
for($i=0; $i -lt $originalcsv.count; $i++){
  $originalcsv[$i] | Select-Object *, @{l="$currenttime"; e={$newcolumnobj.item($i)}}
}