表1
合同号、厂商名称、描述、用户
表2
合同号、产品、部门
匹配条件:对于所有与表1匹配的Contract ID,获取它们的Vendor Name和Contract ID
查询结果输出:合同ID(不同),供应商名称
下面的代码使用内部连接,需要相同的输出,而不使用连接作为linq查询
select table1.Contract ID,table1.Vendor Name ,table2.Contract ID
from table1 as s1
inner join table2 as s2
on s1.Contract ID=s2.Contract ID
\
Thanks in Advance
考虑到您只需要Join选项来选择distinct,您可以使用下面的内部查询逻辑来编写LINQ选择合同编号,供应商名称在哪里Contracterid在(Select distinct contractor id from table2)
这里假设contractorId是表1中的主键
如果我理解正确的话,您想要检索包含合同Id和供应商名称的对象集合,没有重复,其合同Id在表2中找到。
不清楚你是在使用Linq对对象,Linq对实体,还是任何其他Linq风格,这将对如何最好地为特定目的构造查询产生有意义的影响。
但是作为第一个提示,这里有一种不使用Linq连接来执行此操作的方法:
// Get a list of all distinct Contract Ids in Table 2
var AllTable2ContractIds = Table2
.Select(e => e.ContractId)
.Distinct()
.ToList();
// With Table 1
// Keep only entries whose contract Id is found in the list just contructed above.
// transform it to keep Contract Id and Vendor Name.
// The use of ToList() at the end is not mandatory.
// It depends if you want to materialize the result or not.
var Result = Table1
.Where(e => AllTable2ContractIds.Contains(e.ContractId))
.Select(e => new
{
e.ContractId,
e.VendorName
})
.ToList();