Currently I need to interleave to strings into a singe list, yet am at a loss re how to do it.
The code I'm using currently is this (I haven't gotten very far):
public PartialViewResult Interleave(string details, string ids)
{
List<string> detailList = details.Split(',').ToList();
List<string> idlist = ids.Split(',').ToList();
return PartialView("_ConceptDetail1", detailList)
}
Is there a standard way to interleave the lists?
Maybe you want to zip both together, you can use Enumerable.Zip
then:
String[] details = details.Split(',');
String[] ids = ids.Split(',');
List<String> idDetails = ids.Zip(details, (id, detail) => id + " " + detail)
.ToList();
Based on Zip
's implementation, I supposed I would make my own extension method :
static IEnumerable<T> Interleave<T>(this IEnumerable<T> first, IEnumerable<T> second)
{
using (IEnumerator<T> enumerator = first.GetEnumerator())
{
using (IEnumerator<T> enumerator2 = second.GetEnumerator())
{
while (enumerator.MoveNext() && enumerator2.MoveNext())
{
yield return enumerator.Current;
yield return enumerator2.Current;
}
}
}
yield break;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.