Skip to content

Commit 067dd42

Browse files
committed
Add arternative extension method for WithIndex
1 parent 4839a72 commit 067dd42

3 files changed

Lines changed: 167 additions & 0 deletions

File tree

src/DomainCommonExtensions/ArraysExtensions/EnumerableExtensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using DomainCommonExtensions.CommonExtensions;
1818
using DomainCommonExtensions.CommonExtensions.TypeParam;
1919
using DomainCommonExtensions.DataTypeExtensions;
20+
using DomainCommonExtensions.Models;
2021
using System;
2122
using System.Collections.Generic;
2223
using System.Collections.ObjectModel;
@@ -344,6 +345,23 @@ public static bool IsNullOrEmptyEnumerable<T>(this IEnumerable<T> source)
344345
}
345346
#endif
346347

348+
/// <summary>
349+
/// Format list to list with item index
350+
/// </summary>
351+
/// <param name="self">Input list</param>
352+
/// <returns></returns>
353+
/// <typeparam name="T">List type</typeparam>
354+
/// <remarks></remarks>
355+
public static IEnumerable<WithIndexModel<T>> WithIndexModel<T>(this IEnumerable<T> self)
356+
{
357+
return self?.Select(
358+
(item, index) => new WithIndexModel<T>()
359+
{
360+
Index = index,
361+
Item = item
362+
}) ?? new List<WithIndexModel<T>>();
363+
}
364+
347365
/// <summary>
348366
/// Generates a string from a list of values with the given delimiter
349367
/// </summary>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// ***********************************************************************
2+
// Assembly : RzR.Shared.Extensions.DomainCommonExtensions
3+
// Author : RzR
4+
// Created On : 2025-07-15 18:33
5+
//
6+
// Last Modified By : RzR
7+
// Last Modified On : 2025-07-15 18:33
8+
// ***********************************************************************
9+
// <copyright file="WithIndexModel.cs" company="RzR SOFT & TECH">
10+
// Copyright © RzR. All rights reserved.
11+
// </copyright>
12+
//
13+
// <summary>
14+
// </summary>
15+
// ***********************************************************************
16+
17+
namespace DomainCommonExtensions.Models
18+
{
19+
/// -------------------------------------------------------------------------------------------------
20+
/// <summary>
21+
/// A data Model for the with index enumerable extension method.
22+
/// </summary>
23+
/// <typeparam name="T">Generic type parameter.</typeparam>
24+
/// =================================================================================================
25+
public struct WithIndexModel<T>
26+
{
27+
/// -------------------------------------------------------------------------------------------------
28+
/// <summary>
29+
/// Gets or sets the zero-based index of this object.
30+
/// </summary>
31+
/// <value>
32+
/// The index.
33+
/// </value>
34+
/// =================================================================================================
35+
public int Index { get; set; }
36+
37+
/// -------------------------------------------------------------------------------------------------
38+
/// <summary>
39+
/// Gets or sets the item.
40+
/// </summary>
41+
/// <value>
42+
/// The item.
43+
/// </value>
44+
/// =================================================================================================
45+
public T Item { get; set; }
46+
}
47+
}

src/tests/DataTypeTests/DataTests/EnumerableTests.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
// </summary>
1515
// ***********************************************************************
1616

17+
using System.Collections.Generic;
1718
using System.Linq;
19+
using DataTypeTests.Models;
1820
using DomainCommonExtensions.ArraysExtensions;
1921
using Microsoft.VisualStudio.TestTools.UnitTesting;
2022

@@ -65,5 +67,105 @@ public void ChunkArray_Chunked_20_Test()
6567
Assert.AreEqual(1, chunks.Count());
6668
Assert.AreEqual(12, chunks[0].Count());
6769
}
70+
71+
[TestMethod]
72+
public void WithIndex_Null_Test()
73+
{
74+
List<Models.IdNameActiveModel> array = null;
75+
76+
var arrayWithIndex = array.WithIndex();
77+
78+
Assert.IsNotNull(arrayWithIndex);
79+
Assert.AreEqual(0, arrayWithIndex.Count());
80+
}
81+
82+
[TestMethod]
83+
public void WithIndex_Empty_Test()
84+
{
85+
var array = new List<Models.IdNameActiveModel>();
86+
87+
var arrayWithIndex = array.WithIndex();
88+
89+
Assert.IsNotNull(arrayWithIndex);
90+
Assert.AreEqual(0, arrayWithIndex.Count());
91+
}
92+
93+
[TestMethod]
94+
public void WithIndex_Test()
95+
{
96+
var array = new List<Models.IdNameActiveModel>()
97+
{
98+
new IdNameActiveModel()
99+
{
100+
Id = 0,
101+
Name = "Test 0",
102+
IsActive = true
103+
},
104+
new IdNameActiveModel()
105+
{
106+
Id = 1,
107+
Name = "Test 1",
108+
IsActive = false
109+
}
110+
};
111+
112+
var arrayWithIndex = array.WithIndex();
113+
114+
Assert.IsNotNull(arrayWithIndex);
115+
Assert.AreEqual(2, arrayWithIndex.Count());
116+
}
117+
118+
[TestMethod]
119+
public void WithIndexModel_Null_Test()
120+
{
121+
List<Models.IdNameActiveModel> array = null;
122+
123+
var arrayWithIndex = array.WithIndexModel();
124+
125+
Assert.IsNotNull(arrayWithIndex);
126+
Assert.AreEqual(0, arrayWithIndex.Count());
127+
}
128+
129+
[TestMethod]
130+
public void WithIndexModel_Empty_Test()
131+
{
132+
var array = new List<Models.IdNameActiveModel>();
133+
134+
var arrayWithIndex = array.WithIndexModel();
135+
136+
Assert.IsNotNull(arrayWithIndex);
137+
Assert.AreEqual(0, arrayWithIndex.Count());
138+
}
139+
140+
[TestMethod]
141+
public void WithIndexModel_Test()
142+
{
143+
var array = new List<Models.IdNameActiveModel>()
144+
{
145+
new IdNameActiveModel()
146+
{
147+
Id = 0,
148+
Name = "Test 0",
149+
IsActive = true
150+
},
151+
new IdNameActiveModel()
152+
{
153+
Id = 1,
154+
Name = "Test 1",
155+
IsActive = false
156+
}
157+
};
158+
159+
var arrayWithIndex = array.WithIndexModel();
160+
161+
Assert.IsNotNull(arrayWithIndex);
162+
Assert.AreEqual(2, arrayWithIndex.Count());
163+
164+
var last = arrayWithIndex.Last();
165+
Assert.IsNotNull(last);
166+
Assert.IsNotNull(last.Item);
167+
Assert.AreEqual(1, last.Item.Id);
168+
Assert.AreEqual(1, last.Index);
169+
}
68170
}
69171
}

0 commit comments

Comments
 (0)