LINQ “zip” in String Array

前端 未结 5 805
春和景丽
春和景丽 2020-12-10 14:15

Say there are two arrays:

String[] title = { \"One\",\"Two\",\"three\",\"Four\"};
String[] user = { \"rob\",\"\",\"john\",\"\"};

I need to

5条回答
  •  孤城傲影
    2020-12-10 14:29

    As a complement to the already posted answers, here is a solution without using the Zip method. This assumes that both arrays is of same length.

            var pairs = from idx in Enumerable.Range(0, title.Length)
                        let pair = new {Title = title[idx], User = user[idx]}
                        where !String.IsNullOrEmpty(pair.User)
                        select String.Format("{0}:{1}", pair.Title, pair.User);
    

提交回复
热议问题