如何将转换器映射属性中的ienumerable转换为列表

时间:2016-08-25 00:05:42

标签: c# asp.net .net c#-4.0 linq-to-sql

我有一个对象如下。

public class person
{

  public int ID { get; set; }
  public string name { get; set;}
}

我有一个将Ienumerable转换为此对象列表的转换器。 (通过从那里映射这些ID和名称(它们也只有ID和名称)。怎么办?

1 个答案:

答案 0 :(得分:2)

如果你有

.ToList()

...对于任何IEnumerable<>

,您应自动拥有IEnumerable<person> people = SomeMethod(); List<person> peopleList = people.ToList(); 扩展名方法
import datetime as dt

if __name__ == "__main__":
    weekend_index = (6, 5)  # Sunday, Saturday
    requested_range = (dt.datetime(2016, 7, 9, 0, 0), dt.datetime(2016, 8, 11, 0, 0))

    start, end = requested_range
    sun, sat = weekend_index
    cur = start
    my_range = []

    while cur < end:
            cr = []
            cr.append(cur)
            cur = end if end < cur+dt.timedelta(days=6) else (cur+dt.timedelta(days=(sun if cur.weekday() == sun else (sat-cur.weekday()))))
            cr.append(cur)
            cur += dt.timedelta(days=1)
            my_range.append(cr)

print(my_range)  # Returns:
# [[datetime.datetime(2016, 7, 9, 0, 0), datetime.datetime(2016, 7, 9, 0, 0)],
#  [datetime.datetime(2016, 7, 10, 0, 0), datetime.datetime(2016, 7, 16, 0, 0)],
#  [datetime.datetime(2016, 7, 17, 0, 0), datetime.datetime(2016, 7, 23, 0, 0)],
#  [datetime.datetime(2016, 7, 24, 0, 0), datetime.datetime(2016, 7, 30, 0, 0)],
#  [datetime.datetime(2016, 7, 31, 0, 0), datetime.datetime(2016, 8, 6, 0, 0)],
#  [datetime.datetime(2016, 8, 7, 0, 0), datetime.datetime(2016, 8, 11, 0, 0)]]