2

I have the following code:

    public IEnumerable<Content.Grid> DetailsBase(string pk)
    {
        var data = contentRepository.GetPk(pk);
        var refType = this.GetRefType(pk);
        var refStat = this.GetRefStat(pk);
        var type = referenceRepository.GetPk(refType);
        var stat = referenceRepository.GetPk(refStat);
        var dataOut =
        from d in data
        join s in stat on d.Status equals s.RowKey into statuses
        from s in statuses.DefaultIfEmpty()
        join t in type on d.Type equals t.RowKey into types
        from t in types.DefaultIfEmpty()
        select new Content.Grid
        {
            PartitionKey = d.PartitionKey,
            RowKey = d.RowKey,
            Order = d.Order,
            Title = d.Title,
            Status = s == null ? "" : s.Value,
            StatusKey = d.Status,
            Type = t == null ? "" : t.Value,
            TypeKey = d.Type
        };
        return dataOut;
    }

and this class:

    public class Grid
    {
        public string PartitionKey { get; set; }
        public string RowKey { get; set; }
        // Counter
        public int Row { get; set; }
        //
        public int Order { get; set; }
        public string Title { get; set; }
        public string Status { get; set; }
        public string StatusKey { get; set; }
        public string Type { get; set; }
    }

Is there a way that I can set the value of Row to increment. In another select I used this:

        return dataIn
            .OrderBy(item => item.Order)
            .Select((t, index) => new Content.Grid()
            {
                PartitionKey = t.PartitionKey,
                RowKey = t.RowKey,
                Row = index + 1,

Is there a way I could use something similar for my first select?

Update:

I have the following which seems to work but not sure how I can join these two selects:

    public IList<Content.Grid> GetContentGrid(string pk)
    {
        var data =
        from d in contentRepository.GetPk(pk)
        join s in referenceRepository.GetPk(this.GetRefStat(pk)) 
        on   d.Status equals s.RowKey into statuses
        from s in statuses.DefaultIfEmpty()
        join t in referenceRepository.GetPk(this.GetRefType(pk))
        on   d.Type equals t.RowKey into types
        from t in types.DefaultIfEmpty()
        select new Content.Grid
        {
            PartitionKey = d.PartitionKey,
            RowKey = d.RowKey,
            Order = d.Order,
            Title = d.Title,
            Status = s == null ? "" : s.Value,
            StatusKey = d.Status,
            Type = t == null ? "" : t.Value,
            TypeKey = d.Type,
            Link = d.Link,
            Notes = d.Notes,
            TextLength = d.TextLength,
            // AuditableTable
            Created = d.Created ?? new DateTime(2012, 1, 1),
            CreatedBy = d.CreatedBy ?? "n/a",
            Modified = d.Modified ?? new DateTime(2012, 1, 1),
            ModifiedBy = d.ModifiedBy ?? "n/a" 
        };
        return data
            .OrderBy(item => item.Order)
            .Select((t, index) => new Content.Grid()
            {
                PartitionKey = t.PartitionKey,
                RowKey = t.RowKey,
                Row = index + 1,
                Order = t.Order,
                Title = t.Title,
                Status = t.Status,
                StatusKey = t.StatusKey,
                Type = t.Type,
                TypeKey = t.TypeKey,
                Link = t.Link,
                Notes = t.Notes,
                TextLength = t.TextLength,
            })
            .ToList();
    }
Alan2
  • 23,493
  • 79
  • 256
  • 450
  • Possible duplicate of [How To Project a Line Number Into Linq Query Results](http://stackoverflow.com/questions/365086/how-to-project-a-line-number-into-linq-query-results) – Michael Freidgeim Jul 13 '16 at 03:23

1 Answers1

8

You can't specify an index-related projection in a query expression, but you could do something like:

    var dataOut = from d in data
                  join s in stat on d.Status equals s.RowKey into statuses
                  from s in statuses.DefaultIfEmpty()
                  join t in type on d.Type equals t.RowKey into types
                  from t in types.DefaultIfEmpty()
                  select new { d, s, t };
    return dataOut.Select((x, index) => new Content.Grid {
                PartitionKey = x.d.PartitionKey,
                RowKey = x.d.RowKey,
                Order = x.d.Order,
                Title = x.d.Title,
                Status = x.s == null ? "" : x.s.Value,
                StatusKey = x.d.Status,
                Type = x.t == null ? "" : x.t.Value,
                TypeKey = x.d.Type,
                Row = index
           };

Note that without specifying any ordering, however, this may fail - or just give unreliable ordering.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I did some work on this and came up with an update which I added to the question. Let me know what you think of this? Not sure if I can use my updated suggestion and/or if I can combine the two selects. Thanks. – Alan2 Aug 19 '12 at 08:42
  • @Gemma: Yes, looks like it should be fine. You can absolutely combine selects - at least in general. (Different LINQ providers may pose problems, of course.) – Jon Skeet Aug 19 '12 at 13:52