如何在DataGridView C#中存储变量并选择位置

时间:2019-09-25 06:33:38

标签: c#

我有这组代码,它将返回SelisihLoadSchAct列的总和,其中列SchLoadSubVessel等于变量SubVesselName和列SchLoadVessel等于SubVoyageNo到数据类型的变量双重SisaSelisihSchLoad

double SisaSelisihSchLoad = dgv_bulkclink.Rows.Cast<DataGridViewRow>()
.Where(r => Convert.ToString(r.Cells["SchLoadSubVessel"].Value) == SubVesselName && Convert.ToString(r.Cells["SchLoadVessel"].Value) == SubVoyageNo)
.Sum(t => Convert.ToInt32(t.Cells["SelisihLoadSchAct"].Value));

现在问题是,我该如何选择?不总和?我尝试更改总和以选择它,但是它返回错误,或者我在选择语句上做错了什么?

编辑:已更新问题

我如何像查询中一样使用%?例如SELECT Column FROM Table WHERE Column1 LIKE 'SubVesselName%'

3 个答案:

答案 0 :(得分:1)

如果您想要所有单个值,则需要对要获取的值调用Select(),然后将其放入IEnumerable<T>中。

我在下面使用var来让编译器为我选择类型,因此,如果类型更改,我不必更新它。如果要指定,则可以是从IEnumerable<T>派生的任何内容(例如List<T>decimal[]等)

var SisaSelisihSchLoads = dgv_bulkclink.Rows.Cast<DataGridViewRow>()
    .Where(r => Convert.ToString(r.Cells["SchLoadSubVessel"].Value) == SubVesselName 
             && Convert.ToString(r.Cells["SchLoadVessel"].Value) == SubVoyageNo)
    .Select(t => Convert.ToDecimal(t.Cells["SelisihLoadSchAct"].Value));

编辑:

根据您所编辑的问题,您可以使用字符串方法Where()SELECT * FROM [TABLE] WHERE [COLUMN] like 'value%'更改为与.StartsWith()类似,如下所示:

var SisaSelisihSchLoads = dgv_bulkclink.Rows.Cast<DataGridViewRow>()
    .Where(r => Convert.ToString(r.Cells["SchLoadSubVessel"].Value).StartsWith(SubVesselName) 
             && Convert.ToString(r.Cells["SchLoadVessel"].Value) == SubVoyageNo)
    .Select(t => Convert.ToDecimal(t.Cells["SelisihLoadSchAct"].Value));

答案 1 :(得分:1)

根据您的情况

Sum() => return double
Select() => return IEnumerable<double>

示例

// Store sum of SelisihLoadSchAct
double SisaSelisihSchLoad = dgv_bulkclink.Rows.Cast<DataGridViewRow>()
.Where(r => Convert.ToString(r.Cells["SchLoadSubVessel"].Value) == SubVesselName && Convert.ToString(r.Cells["SchLoadVessel"].Value) == SubVoyageNo)
.Sum(t => Convert.ToInt32(t.Cells["SelisihLoadSchAct"].Value));

// Store list of SelisihLoadSchAct
IEnumerable<int> SisaSelisihSchLoad = dgv_bulkclink.Rows.Cast<DataGridViewRow>()
.Where(r => Convert.ToString(r.Cells["SchLoadSubVessel"].Value) == SubVesselName && Convert.ToString(r.Cells["SchLoadVessel"].Value) == SubVoyageNo)
.Select(t => Convert.ToInt32(t.Cells["SelisihLoadSchAct"].Value));

答案 2 :(得分:1)

C#是强类型的,因此,如果您只是从Sum更改为Select,则在尝试将集合分配给double时会得到错误的类型。

您可以将Sum更改为Select,但也需要更改SisaSelisihSchLoad的类型。您可以使用var并让Visual自行解决, 或者您可以声明类似double[](或List<double>)的内容,并在Select之后调用附加方法ToArray(或ToList)。

var SisaSelisihSchLoad = dgv_bulkclink.Rows.Cast<DataGridViewRow>()
.Where(r => Convert.ToString(r.Cells["SchLoadSubVessel"].Value) == SubVesselName && Convert.ToString(r.Cells["SchLoadVessel"].Value) == SubVoyageNo)
.Select(t => Convert.ToInt32(t.Cells["SelisihLoadSchAct"].Value));

double[] SisaSelisihSchLoad = dgv_bulkclink.Rows.Cast<DataGridViewRow>()
.Where(r => Convert.ToString(r.Cells["SchLoadSubVessel"].Value) == SubVesselName && Convert.ToString(r.Cells["SchLoadVessel"].Value) == SubVoyageNo)
.Select(t => Convert.ToInt32(t.Cells["SelisihLoadSchAct"].Value)).ToArray();

Lisst<double> SisaSelisihSchLoad = dgv_bulkclink.Rows.Cast<DataGridViewRow>()
.Where(r => Convert.ToString(r.Cells["SchLoadSubVessel"].Value) == SubVesselName && Convert.ToString(r.Cells["SchLoadVessel"].Value) == SubVoyageNo)
.Select(t => Convert.ToInt32(t.Cells["SelisihLoadSchAct"].Value)).ToList();