Linq“报告”有关列表属性的问题

发布于 2024-10-08 03:40:19 字数 1924 浏览 2 评论 0原文

我有一个包含列表属性的实体对象。我想将列表值扩展到右侧。作为 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 技术交流群。

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

发布评论

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

评论(1

紫南 2024-10-15 03:40:19

这取决于您是想在内存中还是在数据库中运行查询。在任何情况下,您都需要返回一个包含结果的“动态”部分的列表,因为您无法动态生成匿名类型的成员(并且使用它们会很困难)。

在内存中(如您的示例),您可以编写以下查询:

// Find names of all events (for all people)
var allNames = 
  (from p in people
    from e in p.Events
    select e.EventName).Distinct();

// Find events for every person
var res = 
  from p in people
  let known = p.Events.ToDictionary(e => e.EventName)
  select new { 
    p.Name, 
    Events = allNames.Select(n => 
      known.ContainsKey(n)?known[n].EventLocation:"N/A")
  };

第一个查询获取所有事件的名称(稍后我们将使用它来查找每个人的所有事件的值)。第二个查询迭代所有人。它首先创建包含事件的字典(以便在内存中快速查找),然后迭代所有事件名称并尝试在字典中查找它们(如果未找到则返回“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:

// Find names of all events (for all people)
var allNames = 
  (from p in people
    from e in p.Events
    select e.EventName).Distinct();

// Find events for every person
var res = 
  from p in people
  let known = p.Events.ToDictionary(e => e.EventName)
  select new { 
    p.Name, 
    Events = allNames.Select(n => 
      known.ContainsKey(n)?known[n].EventLocation:"N/A")
  };

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).

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