Linq“报告”有关列表属性的问题
我有一个包含列表属性的实体对象。我想将列表值扩展到右侧。作为 LINQ 的新手,我不知道如何做到这一点。我可以强类型化一个对象,但是我必须在编译时知道计数/值,并且我想让它更加动态。
我想要的输出类似于:
Name Demo1 ; 演示2 演示3
人名1 TX TX
人名2 TX &n bsp; ; 好的
人名3 TX TX &n bsp; ;确定
主类
public Main()
{
List<Event> events = new List<Event>();
events.Add(new Event()
{
EventDate = DateTime.Now,
EventLocation = new Location() { State = "TX" },
EventName = "Demo1"
});
events.Add(new Event()
{
EventDate = DateTime.Now,
EventLocation = events[0].EventLocation,
EventName = "Demo2"
});
events.Add(new Event()
{
EventDate = DateTime.Now,
EventLocation = new Location() { State = "OK" },
EventName = "Demo3"
});
List<Person> people = new List<Person>();
Person person1 = new Person();
person1.Name = "Person Name1";
person1.Events.Add(events[0]);
person1.Events.Add(events[1]);
Person person2 = new Person();
person2.Name = "Person Name2";
person2.Events.Add(events[0]);
person2.Events.Add(events[2]);
Person person3 = new Person();
person3.Name = "Person Name3";
person3.Events.Add(events[0]);
person3.Events.Add(events[1]);
person3.Events.Add(events[2]);
people.Add(person1);
people.Add(person2);
people.Add(person3);
}
I have an entity object that contains a list property. I'd like to expand the list values to the right. Being new to LINQ, I'm not sure how to do this. I could strongly type an object, but then I'd have to know the count/values at compile time and I'd like to make it more dynamic.
The output that I'm wanting is something like:
Name Demo1 Demo2 Demo3
Person Name1 TX TX
Person Name2 TX OK
Person Name3 TX TX OK
Main Class
public Main()
{
List<Event> events = new List<Event>();
events.Add(new Event()
{
EventDate = DateTime.Now,
EventLocation = new Location() { State = "TX" },
EventName = "Demo1"
});
events.Add(new Event()
{
EventDate = DateTime.Now,
EventLocation = events[0].EventLocation,
EventName = "Demo2"
});
events.Add(new Event()
{
EventDate = DateTime.Now,
EventLocation = new Location() { State = "OK" },
EventName = "Demo3"
});
List<Person> people = new List<Person>();
Person person1 = new Person();
person1.Name = "Person Name1";
person1.Events.Add(events[0]);
person1.Events.Add(events[1]);
Person person2 = new Person();
person2.Name = "Person Name2";
person2.Events.Add(events[0]);
person2.Events.Add(events[2]);
Person person3 = new Person();
person3.Name = "Person Name3";
person3.Events.Add(events[0]);
person3.Events.Add(events[1]);
person3.Events.Add(events[2]);
people.Add(person1);
people.Add(person2);
people.Add(person3);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于您是想在内存中还是在数据库中运行查询。在任何情况下,您都需要返回一个包含结果的“动态”部分的列表,因为您无法动态生成匿名类型的成员(并且使用它们会很困难)。
在内存中(如您的示例),您可以编写以下查询:
第一个查询获取所有事件的名称(稍后我们将使用它来查找每个人的所有事件的值)。第二个查询迭代所有人。它首先创建包含事件的字典(以便在内存中快速查找),然后迭代所有事件名称并尝试在字典中查找它们(如果未找到则返回“N/A”)。
It depends on whether you want to run the query in memory or in databse. In any case, you'll need to return a list with the "dynamic" part of the results, because you cannot dynamically generate members of anonymous types (and working with them would be difficult).
In memory (as in your example), you can write the following query:
The first query gets names of all events (we use it later to find a value for all event for every person). The second query iterates over all people. It first creates dictionary with events (for fast lookup in memory) and then iterates over all event names and tries to find them in the dictionary (returning "N/A" if it is not found).