Join collection of objects into comma-separated string

前端 未结 11 1327
无人共我
无人共我 2020-12-07 19:22

In many places in our code we have collections of objects, from which we need to create a comma-separated list. The type of collection varies: it may be a DataTable from whi

11条回答
  •  伪装坚强ぢ
    2020-12-07 19:57

    I love Matt Howells answer in this post:

    I had to make it into an extension:

    public static string ToCsv(this IEnumerable things, Func toStringMethod)
    

    Usage: [ I am getting all the emails and turning them into a csv string for emails ]:

    var list = Session.Find("from User u where u.IsActive = true").Cast();
    
    return list.ToCsv(i => i.Email);
    

提交回复
热议问题