所以我有一个名为Version
的对象它包含STATES列表和DATES列表
我想先:
以下是我的尝试:
Date = Version.States.GroupBy(v => v.Date).Select(vh => vh.ToString()).ToString(),
State = Version.States.GroupBy(s => s.State).Select(st => st.ToString()).ToString()
有人可以帮助我吗?
答案 0 :(得分:1)
您无需使用GroupBy
,您可以使用Distinct
获取更高效的唯一项目。然后,您可以使用String.Join
来构建字符串:
var uniqueStates = Version.States.Select(vs => vs.State).Distinct();
var uniqueDates = Version.Dates.Select(vd => vd.Date).Distinct();
string commaSeparatedStates = String.Join(",", uniqueStates);
string commaSeparatedDates = String.Join(",", uniqueDates);
答案 1 :(得分:0)
您可以使用string.Join
:
string all = string.Join( ","
, Version.States.GroupBy
( s => s.State )
.Select(st => st.ToString())
)
);
答案 2 :(得分:0)
您需要使用string.Join
试试这个。
var Dates = string.Join(",",Version.States.GroupBy(v => v.Date).Select(vh => vh.ToString()));
var States = string.Join(",",Version.States.GroupBy(s => s.State).Select(st => st.ToString()));
答案 3 :(得分:0)
试试这个:
var states = Version.States.Aggregate(string.Empty, (c, state) => c + state.ToString() + ",").TrimEnd(new []{','});
var dates = Version.Dates.Aggregate(string.Empty, (c, date) => c + date.ToString() + ",").TrimEnd(new []{','});