使用C#</t>将索引字段添加到LINQ List <t>结果

时间:2009-08-17 23:03:44

标签: c# linq

我正在使用通用列表。例如(带有一些属性):

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;

但现在它在索引列上显示为空白。我想得到这样的东西:

alt text

这是关于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;**

我该如何解决这个问题?

4 个答案:

答案 0 :(得分:2)

看起来您需要将_grouppedResto分配给匿名类型,如:

var yourSequence = _groupedResto.Select((value, index) => new { index = index, value = value });
dgvHeader.DataSource = yourSequence;

我很乐意为您提供的任何细节提供帮助。 祝你好运,希望这有助于:)

答案 1 :(得分:0)

要将索引与list元素的属性组合,您需要将select语句更改为以下内容:

_grouppedResto.Select(
  (value, index) => new {
    index = index, propertyA = value.propertyA, propertyB = value.propertyB
  }
);

答案 2 :(得分:0)

好吧,我用这种方式来解决这个问题...(我认为这是最糟糕的方式,因为你必须修改域类)

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;

答案 3 :(得分:0)

int theIndex = 1;

foreach(var x in _grouppedResto)
{
  x.index = theIndex;
  theIndex += 1;
}