将索引字段添加到LINQ列表 使用C#的结果
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将索引字段添加到LINQ列表 使用C#的结果相关的知识,希望对你有一定的参考价值。
我正在使用通用列表。例如(带有一些属性):
public class randomList
{
public string propertyA { get; set; }
public string propertyB { get; set; }
public string propertyC { get; set; }
}
所以在我的检索查询中,我曾写过以下内容:
_grouppedResto.Select((value, index) => new { index = index, value = value });
dgvHeader.DataSource = _grouppedResto;
但现在它在索引列上显示为空白。我想得到这样的东西:
这是关于datagrid的全部内容:
this.dgvHeader.AllowUserToAddRows = false;
this.dgvHeader.AllowUserToDeleteRows = false;
this.dgvHeader.AllowUserToResizeRows = false;
this.dgvHeader.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dgvHeader.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.FECRGV,
this.TDORGV,
this.NDORGV,
this.RUCCLI,
this.RAZCLI,
this.VVTRGVS,
this.IGVRGVS,
this.TOTRGVS,
this.TCARGV,
this.VVTRGVD,
this.IGVRGVD,
this.TOTRGVD});
this.dgvHeader.Location = new System.Drawing.Point(12, 128);
this.dgvHeader.Name = "dgvCACD";
this.dgvHeader.Size = new System.Drawing.Size(1265, 611);
this.dgvHeader.TabIndex = 13;
**dgvHeader.AutoGenerateColumns = false;**
我该如何解决这个问题?
答案
看起来您需要将_grouppedResto分配给匿名类型,如下所示:
var yourSequence = _groupedResto.Select((value, index) => new { index = index, value = value });
dgvHeader.DataSource = yourSequence;
我很乐意帮助您提供您提供的任何细节。祝你好运,希望这有助于:)
另一答案
要将索引与列表元素的属性组合,您需要将select语句更改为以下内容:
_grouppedResto.Select(
(value, index) => new {
index = index, propertyA = value.propertyA, propertyB = value.propertyB
}
);
另一答案
好吧,我用这种方式来解决这个问题...(我认为这是最糟糕的方式,因为你必须修改域类)
public class RGVCAFAC
{
public int index { get; set; }
public string CODEAUX { get; set; }
public string FECRGV { get; set; }
public string TDORGV { get; set; }
public string MONRGV { get; set; }
public string NDORGV { get; set; }
public string RUCCLI { get; set; }
public string RAZCLI { get; set; }
public string CDCRGV { get; set; }
public string TERRGV { get; set; }
public string PDSRGV { get; set; }
public double VVTRGVS { get; set; }
public double IGVRGVS { get; set; }
public double TOTRGVS { get; set; }
public string TCARGV { get; set; }
public double VVTRGVD { get; set; }
public double IGVRGVD { get; set; }
public double TOTRGVD { get; set; }
public string PACEST { get; set; }
public string CODUNI { get; set; }
public string DIRCLI { get; set; }
public string TELCLI { get; set; }
public int PFLAG { get; set; }
//I have to add the following, so generators are useless :(
public RGVCAFAC()
{
}
public RGVCAFAC(RGVCAFAC x)
{
this.CODEAUX = x.CODEAUX;
this.FECRGV = x.FECRGV;
this.TDORGV = x.TDORGV;
this.MONRGV = x.MONRGV;
this.RUCCLI = x.RUCCLI;
this.RAZCLI = x.RAZCLI;
this.CDCRGV = x.CDCRGV;
this.TERRGV = x.TERRGV;
this.PDSRGV = x.PDSRGV;
this.VVTRGVS = x.VVTRGVS;
this.IGVRGVS = x.IGVRGVS;
this.TOTRGVS = x.TOTRGVS;
this.TCARGV = x.TCARGV;
this.VVTRGVD = x.VVTRGVD;
this.IGVRGVD = x.IGVRGVD;
this.TOTRGVD = x.TOTRGVD;
this.PACEST = x.PACEST;
this.CODUNI = x.CODUNI;
this.DIRCLI = x.DIRCLI;
this.TELCLI = x.TELCLI;
this.PFLAG = x.PFLAG;
}
}
最后:
var yourSequence = _grouppedResto.
Select((value, index) => new RGVCAFAC(value) { index = index+1, rgvcafac = value }).ToList(); //need to begin on 1 not on 0
dgvCabecera.DataSource = yourSequence;
另一答案
int theIndex = 1;
foreach(var x in _grouppedResto)
{
x.index = theIndex;
theIndex += 1;
}
以上是关于将索引字段添加到LINQ列表 使用C#的结果的主要内容,如果未能解决你的问题,请参考以下文章