开发者

Linq: Split contents of a string column and add it to the array returned by Select(o=>o.ColumnName)

开发者 https://www.devze.com 2023-03-15 22:19 出处:网络
A database column contains following values SampleColumnValues T1 T2 T3,T4 T5,T6 T7 T8 When, I try to read these values, it is perfectly possible using the IQueryable.Select() method to get this spec

A database column contains following values

SampleColumnValues

T1

T2

T3,T4

T5,T6

T7

T8

When, I try to read these values, it is perfectly possible using the IQueryable.Select() method to get this specific column from DB and convert it to an array using ToArray().

But, I also need the comma delimited values in the originally returned array as individual array elements.

For now, I have a separate method to do the additional splitting.

Can I achieve the same with stock methods available?

The resultant a开发者_StackOverflowrray I need should be like {T1, T2, T3, T4, T5, T6, T7, T8}


string[] input = new string[] { "T1", "T2", "T3,T4", "T5,T6", "T7", "T8" };

string[] output = input.SelectMany(item => item.Split(',')).ToArray();


This can be done using the Aggregate function...
...Select(o => o.ColumnName).Aggregate((w, n) => w + "," + n).Split(',');

0

精彩评论

暂无评论...
验证码 换一张
取 消