LINQ list to sentence format (insert commas & “and”)

后端 未结 17 1312
天命终不由人
天命终不由人 2021-01-12 23:33

I have a linq query that does something simple like:

var k = people.Select(x=>new{x.ID, x.Name});

I then want a function or linq lambda,

17条回答
  •  不知归路
    2021-01-13 00:25

    This is not pretty but will do the job using LINQ

    string s = string.Join(",", k.TakeWhile(X => X != k.Last()).Select(X => X.Id + ":" + X.Name).ToArray()).TrimEnd(",".ToCharArray()) + " And " + k.Last().Id + ":" + k.Last().Name;
    

提交回复
热议问题