我无法获取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();
}
但我不知道如何与其他表保持同步
答案 0 :(得分:0)
假设您没有使用LINQ to Entities而只使用LINQ to SQL,
将SQL转换为LINQ,
DISTINCT
,TOP
等)作为应用于整个LINQ查询的函数。new { }
)from
,后跟.DefaultIfEmpty()
。COALESCE
。SELECT *
必须替换为select
范围变量或join
s,这是一个包含所有范围变量的匿名对象。SELECT flds
必须替换为select new { ... }
,并创建包含所有所需字段或表达式的匿名对象。对您的查询执行此过程后,我得到:
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 };