【问题标题】:retrieving data by LINQ c#通过LINQ c#检索数据
【发布时间】:2017-03-12 03:34:53
【问题描述】:

我有以下问题

客户和客户 每个客户都与特定客户相关联

-ClientID、ClientName、Customer_custID

-客户 ID、客户名称

我想检索与特定客户相关的每个客户

这是我的尝试

     data=clients.select(c=>c.clientName, 
        c.customerName.where(f>f.customerID==f.customer_custID)

我知道这是非常错误的,但我只是在学习 lamda 表达式和 LINQ,所以请原谅我

【问题讨论】:

  • 您对特定客户了解多少?客户 ID 或客户名称?客户和客户类别是什么样的?
  • 可以data = clients.where(c => c.Customer_custID == customerID);吗?
  • 请阅读更多教程,回来再试一次。这根本不编译
  • 为方便起见,您需要阅读有关延迟加载的更多信息

标签: c# linq


【解决方案1】:

试试这个:

IEnumerable<Client> clients = .... // your clients collection
IEnumerable<Customer> customers = .... // your customers collection

var relatedClients = from cst in customers
                     join cli in clients on cst.CustomerID equals cli.Customer_custID
                     where cst.CustomerID == 0 // your customerID
                     select cli;

【讨论】:

    【解决方案2】:

    我希望我能正确理解您的要求。它假定您在输入查询之前知道要查找的客户 ID。

    var clientsQuery = from clients in Client
                       where clients.Customer_custId == customerId
                       select clients;
    

    然后您可以foreach 覆盖查询结果。

    foreach (var client in clientsQuery) 
    {
        // Do stuff with your clients!
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-29
      • 2013-05-21
      • 1970-01-01
      • 1970-01-01
      • 2013-06-19
      • 1970-01-01
      • 1970-01-01
      • 2019-09-25
      相关资源
      最近更新 更多