关于LINQ内连接一个问题?
今天下午搞了半天没有搞出来,麻烦看下问题在哪里!现在有2张表要内连接,公司company,联络人customer,company有一个字段com_xscode,customer有一个字段c_xscode,把两张表联系起来,如com_xscode:G0000-C0001,c_xscode:G0000-C00001-P0001,G0000-C00001-P0002
show_companyDataContext db_company = new show_companyDataContext(); //公司表
show_customerDataContext db_customer = new show_customerDataContext(); //客户表
//1、调试出错,编译没问题,我点击获取SQL语句发生异常,所以下面这句写法有问题,我目的是要获得来两张连接后所有的字段?
var customer_company = from p in db_company.t_company
orderby p.com_date descending
join c in db_customer.t_customer
on p.com_xscode equals c.c_xscode.Substring(0, 11)
into pc
from x in pc.DefaultIfEmpty()
select new { p.com_en_name, p.com_name,x.c_en_name,x.c_name };
string com_en_name = string.Empty;
foreach (var name in customer_company) //2、调试出错,提示
查询包含对不同数据上下文上所定义项的引用?
{
com_en_name += name.com_en_name;
}
------解决方案--------------------
C# code
//亲,从你的代码来看,是左连接,不是内连接啊
var customer_company = from p in db_company.t_company
join c in db_customer.t_customer
on p.com_xscode equals c.c_xscode.Substring(0, 11)
into pc
from x in pc.DefaultIfEmpty()
orderby p.com_date descending
select new {
com_en_name=p.com_en_name,
com_name=p.com_name,
c_en_name=x==null?"":x.c_en_name,
c_name=x==null?"":x.c_name
};
string com_en_name = string.Empty;
foreach (var name in customer_company.DefaultIfEmpty()) {
com_en_name += name.com_en_name;
}