How to Add an Index Field to Linq Results

How do you add an index field to Linq results

Don't use a query expression. Use the overload of Select which passes you an index:

var list = FileList.Select((file, index) => new { Index=index, Filename=file });

adding index to linq query result

Once you get away from the query syntax, you'll find a Select overload that gives you the index you're looking for.

var result = MyDataContext
.Where(d => d.Prop == "A")
.AsEnumerable()
.Select((d, i) =>
new MyObject() {
Property1 = d.whatever,
TheIndex = i
});

problemin adding index field to linq results

Simple :

public List<string[]> InstructionsData(IEnumerable<Assets> InstructionsEntry, int currentUserId, int startIndex)
{

return InstructionsEntry.Select((entry,index) => new string[]
{
(startIndex + index + 1).ToString(),
entry.state_Id,
entry.batchRef_Id,
entry.assetCategory_Id,
GetAge(entry.age),
entry.assetStatus_Id,
GetStatusTag(entry.recordStatus ??false),
entry.availbaleQty.ToString(),
entry.createdBy,

}).ToList();

Adding index field to LINQ List T results using C#

It looks like you need to assign _grouppedResto to an anonymous type as in:

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

I'd be happy to help with any particulars you care to provide.
Good luck, hope this helped :)

How do you add an index field to Linq SelectMany results

List<string> animals = new List<string> { "cat", "dog", "donkey" };
List<int> numbers = new List<int> { 10, 20 };
var output = numbers.SelectMany(n => animals.Select(s => s + n))
.Select((g,i) => i + g);

You can do with single SelectMany, but it won't be that nice:

List<string> animals = new List<string> { "cat", "dog", "donkey" };
List<int> numbers = new List<int> { 10, 20 };
var output = numbers.SelectMany((n,ni) => animals.Select((s,si) => ((ni * animals.Count) + si) + s + n))

Adding item index number in Linq

Using method syntax for that part use the Select overload that gets both the item and the index:

var data = (from a in cData.OrderDetails
join b in cData.ItemMaster on a.OrderItemId equals b.ItemID
group a.OrderQty by new { a.OrderItemId, b.ItemName } into g
select new {
ItemName = g.Key.ItemName,
ItemQty = Convert.ToInt16(g.Sum())
}).Select((item,index) => new ConsolidatedOrder {
Sno = index,
ItemName = item.ItemName,
ItemQty = item.ItemQty
};

Notice that I've changed a bit the group by and query syntax select - the items to be grouped are now only the field you desire for the Sum

Add index position Value of an array of array using Linq

If you want to sum up columns' values with a help of Linq:

int[][] source = new int[][] {
new int[] { 1, 2, 3},
new int[] { 3, 4, 5},
new int[] { 5, 4, 3},
};

int maxCol = source.Max(item => item.Length);

var colsSum = Enumerable
.Range(0, maxCol)
.Select(index => source.Sum(item => item.Length > index ? item[index] : 0))
.ToArray(); // let's meaterialize into an array

Test:

Console.Write(string.Join(", ", colsSum));

Outcome:

 9, 10, 11

Summing up lines' values is easier:

// [6, 12, 12]
var linesSum = source
.Select(item => item.Sum())
.ToArray();

If you want total sum:

// 30
var total = source
.Select(item => item.Sum())
.Sum();

or

// 30
var total = source
.SelectMany(item => item)
.Sum();

How can I add an index to a LINQ query when there is a grouping?

I believe you just got the syntax wrong for a lambda with two parameters: .Select((groupings, index) => expression)



Related Topics



Leave a reply



Submit