I'm having a class with several material properties (e.g . temperature). The cross section of a sample is represented by a list of class elements. I only need material properties in multiple methods at specific positions of the cross section or indices, respectively.
At the moment I'm using a Linq-select to first create an IEnumerable of the needed property. Than I'm creating a list using the IEnumerable, where I can select the wanted elements by index. Example (indices is a List with :
var indices = new List<int>() {1, 3, 7, 15, 30, 50};
var Ts = microstructures.Select(x => x.T).ToList();
var list = new List<double>();
for (int i = 0; i < indices.Count; i )
{
list.Add(Ts[indices[i]]);
}
Is there a more efficient way without creating a list to perform this task? microstructures has < 100 elements, indices ~ 10 and the properties of the microstructure can be complex classes themselves.
CodePudding user response:
You may filter the T directly using the where overload that expose the object and it's index. msdn Then replace the Select by a Select many to flattern List<List> to List
public class Toto {
public List<int> T { get; set; }
}
var target = new HashSet<int>{ 1, 3, 7, 15, 30, 50 };
var inputs = Enumerable.Range(0, 10) // generate Data Sample
.Select(x=>new Toto
{
T = Enumerable.Range(100*x,100).ToList()
});
var r = inputs.SelectMany(x => x.T.Where((y,i)=> target.Contains(i)));