Converting IQueryable<object> results to comma delimited string

大城市里の小女人 提交于 2019-12-07 06:43:17

问题


I have a LINQ query that returns all absences for an employee. The first part of the linq statement gets a basic list of the employees details, but I also return an IQueryable list of illnesses related to that absence.

I'd like to somehow convert that IQueryable list to a comma delimited list of Illnesses.

Currently I use (majorly stripped down):

DetailsOfSickness = (
  from t2 in Illnesses
  join ai1 in AbsenceIllnesses on t2.IllnessID equals ai1.IllnessID
  select new { Illness = ", " + t2.IllnessName })

Which returns the list but I'd like the results like: Headache, Flu, Cramps.... etc. Any ideas?


回答1:


You can to use String.Join to create your comma delimited string.

string DetailsOfSickness = 
    String.Join(", ", (
      from t2 in illnesses
      join ai1 in absenceIllnesses on t2.IllnessID equals ai1.IllnessID
      select t2.IllnessName).ToArray());



回答2:


Something like this should do it:

DetailsOfSickness = String.Join(", ", (
  from t2 in Illnesses
  join ai1 in AbsenceIllnesses on t2.IllnessID equals ai1.IllnessID
  select t2.IllnessName).ToArray());
  • Please be aware, non-compiler, non-tested code.


来源:https://stackoverflow.com/questions/1866410/converting-iqueryableobject-results-to-comma-delimited-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!