将对象列表转换为 List>数据的

发布于 2024-09-14 20:53:55 字数 2070 浏览 3 评论 0 原文

我正在尝试在我的系统中使用 jquery DataTables 插件,使用 Web 服务通过 ajax 调用填充数据。

我需要从 web 服务返回的是一个像这样的列表: List>;

我有大约 120 个不同的域类被用作核心数据对象,所以我需要某种系统,我可以从不同的域类获取数据,并以某种自动的方式将它们转换为 List>

我获取数据的方式是这样的(示例代码):

List<Core.Room> coreDataList = Core.GetRoomsInHotel(int hotelID);

我一直在研究DynamicLinq和System.Reflection,但一无所获:(


更新:

我需要从web服务返回的是一个像这样的数组

[
    ["data-col-1","data-col-2","data-col-3","data-col-4"],
    ["data-col-1","data-col-2","data-col-3","data-col-4"],
    ["data-col-1","data-col-2","data-col-3","data-col-4"]
]

:填充此数组,我有大约 120 个具有不同属性的域类的不同列表,并且我需要动态访问每个域类的这些属性:

对于房间,基于如下所示的对象:

public class Room {
    public int ID { get; set; }
    public string Name { get; set; }
    public string Code { get; set; }
    public string LongName { get; set; }
}
[
    ["ID", "Double","DBL"],
    ["ID", "Single","SNG"],
    ["ID", "Suite","SUI"]
]

对于酒店:

public class Hotel {
    public int ID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public int RoomCount { get; set; }
    public int FloorCount { get; set; }
}
[
    ["1","Plaza","153","12"],
    ["2","Astoria","568","25"]
]

我有一个 XML 文件使用列表的配置:

<list id="29">
  <readable_name>Hotels - Roomtypes - List all roomtypes in system</readable_name>
  <no_data_message>No roomtypes found</no_data_message>
  <cols>
    <col width="0" visible="false" datafield="ID" type="key" headertext="ID" />
    <col width="70" visible="true" datafield="Name" type="string" headertext="Name" />
    <col width="30" visible="true" datafield="Type" type="string" headertext="Type" />
  </cols>
</list>

因此,我根据要加载的列表从 XML 文件中提取配置,并根据列表 ID 调用数据存储,并获取我想要的任何域类(房间/酒店)的列表。我浏览返回的列表并生成某种数组,以便可以填充页面上的列表,

我希望这可以澄清一些事情......

I am trying to use the jquery DataTables plugin for my system, using a webservice to populate the data through an ajax call.

What I need to return from the webservice is a List like this: List<List<string>>;

I have about 120 different domain classes being used by this as the core data objects, so I need some kind of system where I can get the data from the different domain classes and transform them into List<List<string>> in a somewhat automatic way.

The way I am getting the data is something like this (example code):

List<Core.Room> coreDataList = Core.GetRoomsInHotel(int hotelID);

I have been looking into DynamicLinq and System.Reflection, but have gotten nowhere :(


UPDATE:

What I need to return from the webservice is an array like this:

[
    ["data-col-1","data-col-2","data-col-3","data-col-4"],
    ["data-col-1","data-col-2","data-col-3","data-col-4"],
    ["data-col-1","data-col-2","data-col-3","data-col-4"]
]

To populate this array, I have about 120 different lists of domain classes with different properties, and I need to access those properties for each one dynamically:

For a room, based on an object looking like this:

public class Room {
    public int ID { get; set; }
    public string Name { get; set; }
    public string Code { get; set; }
    public string LongName { get; set; }
}
[
    ["ID", "Double","DBL"],
    ["ID", "Single","SNG"],
    ["ID", "Suite","SUI"]
]

For a hotel:

public class Hotel {
    public int ID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public int RoomCount { get; set; }
    public int FloorCount { get; set; }
}
[
    ["1","Plaza","153","12"],
    ["2","Astoria","568","25"]
]

I have a XML file with config for the lists:

<list id="29">
  <readable_name>Hotels - Roomtypes - List all roomtypes in system</readable_name>
  <no_data_message>No roomtypes found</no_data_message>
  <cols>
    <col width="0" visible="false" datafield="ID" type="key" headertext="ID" />
    <col width="70" visible="true" datafield="Name" type="string" headertext="Name" />
    <col width="30" visible="true" datafield="Type" type="string" headertext="Type" />
  </cols>
</list>

So I pull the config from the XML file based on which list I want to load. I call the data store based on the list ID, and get a list of whatever domain class I want (Room/Hotel). I go through the returned list and produce kind of array so that the list on the page can be populated.

I hope this clarifies things a bit ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最单纯的乌龟 2024-09-21 20:53:55
List<Core.Room> coreDataList = Core.GetRoomsInHotel(int hotelID); 
List<string> coreDataStrList = coreDataList
                                .Select(room => room.ToString())
                                .ToList();

除此之外,我们将需要有关您的代码的更多信息。

更新:(

List<string> ToStringList<T>(this IEnumerable<T> list)
{
    list.Select(obj => obj.ToString()).ToList();
}

List<Core.Room> coreRoomList = ... 
List<Core.Hotel> coreHotelList = ... 

List<List<string>> ListofLists = new List<List<string>> ();

ListofLists.Add(coreRoomList.ToStringList());
ListofLists.Add(coreHotelList.ToStringList());

如果您一次给我们多于一行代码,这会很有帮助。这 120 个列表是否以某种方式分组?它们有共同的基础吗?

List<Core.Room> coreDataList = Core.GetRoomsInHotel(int hotelID); 
List<string> coreDataStrList = coreDataList
                                .Select(room => room.ToString())
                                .ToList();

Beyond that, we are going to need more information about your code.

UPDATE:

List<string> ToStringList<T>(this IEnumerable<T> list)
{
    list.Select(obj => obj.ToString()).ToList();
}

List<Core.Room> coreRoomList = ... 
List<Core.Hotel> coreHotelList = ... 

List<List<string>> ListofLists = new List<List<string>> ();

ListofLists.Add(coreRoomList.ToStringList());
ListofLists.Add(coreHotelList.ToStringList());

(It would kind-a help if you gave us more that one line of code at a time. Are these 120 lists grouped somehow? Do they have a common base?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文