多对多选择列表数据

时间:2017-12-20 05:56:56

标签: c# sql linq

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

look at the photo

预期结果

Result

 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查询中执行此操作?

1 个答案:

答案 0 :(得分: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
}

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