多对多选择列表数据

问题描述 投票:-3回答:1

我想通过组合图中所示的表来列出linq数据

预期结果

 using (CrmContext db = new Models.CrmContext())
            {
                _result = (from ct in db.CustomerDefination
                           join authorizedin db.authorized
                                    on ct.customerId equals authorized.authorized
                           join authorizedDefination in db.authorizedDefination 
                                    on authorized.authorizedId equals authorizedDefination .authorizedId
                           select new
                           {
                          ///I want to list the authorities belonging to 
 the customer

                           }).ToList();
            }

客户可能有多个权威定义。如何在LINQ查询中执行此操作?

c# sql linq
1个回答
2
投票

我花了太多时间做你的工作,@ IbrahimALPSOY:

  • 谷歌翻译土耳其数据库截图,
  • 使用更多Google翻译了解哪个表包含哪些数据,
  • 理解预期结果 - 哪些字段来自哪些表,
  • 编写示例类来表示数据库,
  • 生成用于测试的样本数据。

在开始编写查询之前,我浪费了30多分钟。下次我不会。下次,您准备好问题,以便所有其他人可以复制您的代码并立即尝试查询。

这是准备:

class Authorisation
{
    public int AuthorisationId; // yetkiliid
    public int AccessId; // unvanid
    public string AuthoriserName;
}
class Access
{
    public int AccessId; // unvanid
    public string AccessName;
}
class Customer
{
    public int CustomerId; // musterid
    public string CustomerName;
}
class Event
{
    public int CustomerId;
    public int AuthorisationId;
}

void Main()
{
    var Customers = new[] {
        new Customer() { CustomerId = 1, CustomerName = "Anne" },
        new Customer() { CustomerId = 2, CustomerName = "Barbara" },
    };
    var Accesses = new[] {
        new Access() { AccessId = 1, AccessName = "Read" },
        new Access() { AccessId = 2, AccessName = "Write" },
    };
    var Authorisations = new[] {
        new Authorisation() { AuthorisationId = 1, AuthoriserName = "The boss", AccessId = 1 }, // The boss can give read rights
        new Authorisation() { AuthorisationId = 2, AuthoriserName = "The boss", AccessId = 2 }, // The boss can give write rights
        new Authorisation() { AuthorisationId = 3, AuthoriserName = "A rookie", AccessId = 1 }, // A new employee can only give read rights
    };
    var Events = new[] {
        new Event() { CustomerId = 1, AuthorisationId = 3 }, // A rookie let Anne read
        new Event() { CustomerId = 1, AuthorisationId = 2 }, // While the boss let Anne write and scolded rookie
        new Event() { CustomerId = 2, AuthorisationId = 1 }, // The boss thinks Barbara can't be trusted with write
    };
}

我用这个代码而不是你的代码,因为你的:

  • 不编译,
  • 难以辨认,
  • 格式错误,
  • 跳过您在屏幕截图中显示的表格,
  • 包含只有您有权访问的上下文的引用。

以下是结果:

如果从包含非唯一键的表开始,则查询变得可行:

from e in Events
    join c in Customers      on e.CustomerId      equals c.CustomerId
    join a in Authorisations on e.AuthorisationId equals a.AuthorisationId
    join s in Accesses       on a.AccessId        equals s.AccessId
select new
{
    e.CustomerId,
    e.AuthorisationId,
    c.CustomerName,
    a.AuthoriserName,
    s.AccessName
}

如果这不是您所需要的,请修改我的“准备”以适合您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.