使用客户ID提取所有数据

时间:2017-12-26 07:53:31

标签: c# sql linq

我无法获取linq查询以从不同的表中检索信息并将其作为列表返回。

例如,客户ID>> 5,我可以在mssql中编写代码:

SELECT *
FROM        crm_customer_authorized AS AUTHORIZEDs
INNER JOIN  crm_authorized_describing AS authorized ON AUTHORIZEDs.authorizedId=authorized.authorizedId
INNER JOIN  crm_customer_describing AS customer ON customer.customerid = AUTHORIZEDs.customerid
INNER JOIN  crm_address_describing AS adress ON adress.customerid = customer.customerid
INNER JOIN  crm_customer_sector AS SEKTORS ON sektors.customerid = customer.customerid
INNER JOIN  crm_sector_describing AS sektor ON sektor.sektorid = sektors.sektorid
INNER JOIN  crm_customer_departman AS departments ON departments.customerid = customer.customerid
INNER JOIN  crm_branch_describing AS department ON department.departmentId= departments.departmentsId
INNER JOIN  address_codes_province AS province ON addresss.provinceid = province.addresscodeid
INNER JOIN  address_codes_provincece AS provincece ON addresss.provinceceid= provincece.section_code
LEFT JOIN   crm_position_describing AS POSITION ON authorized .positionId = position.positionid

输出: Result

如何用linq编写此查询?

客户有多个地址,负责人 负责人,客户可能属于同一部门,但当局不同。

我不知道如何列出客户的多个不同信息的代码

我从客户表中获得了customerIds

var customerIds = from cus in db.customerDescribing 
                  select new { cus.customerId };

然后我在地址表中同步客户

foreach (var item in customerIds )
{
    var linq2 = (from address in db.addressDescribing.Where(x => x.customerId== item.customerId)
                 select new
                 {
                     Address = address.content,
                     CustomerId= address.customerId
                 }).ToArray();

}

但我不知道如何与其他表保持同步

1 个答案:

答案 0 :(得分:0)

假设您没有使用LINQ to Entities而只使用LINQ to SQL,

将SQL转换为LINQ,

  1. 将子选项转换为单独的变量
  2. 以LINQ子句顺序翻译每个子句,将monadic运算符(DISTINCTTOP等)作为应用于整个LINQ查询的函数。
  3. 使用表别名作为范围变量。使用列别名作为匿名类型字段名称。
  4. 对多列使用匿名类型(new { }
  5. 使用连接变量模拟左连接,然后从连接变量执行另一个from,后跟.DefaultIfEmpty()
  6. 用条件运算符和空值测试替换COALESCE
  7. SELECT *必须替换为select范围变量或join s,这是一个包含所有范围变量的匿名对象。
  8. SELECT flds必须替换为select new { ... },并创建包含所有所需字段或表达式的匿名对象。
  9. 对您的查询执行此过程后,我得到:

    var ans = from AUTHORIZEDs in db.customerAuthorized
              join authorized in db.authorizedDescribing on AUTHORIZEDs.authorizedId equals authorized.authorizeId
              join customer in db.customerDescribing on AUTHORIZEDs.customerid equals customer.customerid
              join adress in db.addressDescribing on customer.customerid equals adress.customerid
              join SEKTORS in db.customerSector on customer.customerid equals SEKTORS.customerid
              join sektor in db.sectorDescribing on SEKTORS.sektorid equals sektor.sektorid
              join departments in db.customerDepartman on customer.customerid equals departments.customerid
              join department in db.branchDescribing on departments.departmentsId equals department.departmentId
              join province in db.addressCodesDrovince on adress.provinceid equals province.addresscodeid
              join provincece in db.addressCodesProvincece on adress.provinceceid equals provincece.section_code
              join POSITION in db.positionDescribing on authorized.positionId equals POSITION.positionid into POSITIONj
              from POSITION in POSITIONj.DefaultIfEmpty()
              select new { AUTHORIZEDs, authorized, customer, adress, SEKTORS, sektor, departments, department, province, provincece, POSITION };