如何在LINQ中编写自联接 [英] how to write self join in LINQ

查看:64
本文介绍了如何在LINQ中编写自联接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在LINQ中编写自联接?我试过这个:

 来自 coa   GL0_ChartOfAccounts 
< span class =code-sdkkeyword> join c 在coa.MainAccount上的GL0_ChartOfAccounts等于c.ControlAccount into cs
来自 ca cs.DefaultIfEmpty()
< span class =code-sdkkeyword>选择 new
{
MainAccount = coa.MainAccount,
Title = coa.Title,
ControlLevel = coa.ControlLevel,
ControlAccount = coa.ControlAccount,
ControlTitle = ca.Title
}

不幸的是,这给了一个匿名类型错误。



为了澄清这一点,假设有三列:

 MainAccount 
标题
ParentAccount

现在我想得到帐户的名称或名称,而不是ParentAccount(int)我该怎么办?在LINQ中使用这个吗?

解决方案

var q =(来自coocha in Glochartofaccount

加入c in glochartofaccount on coamainaccount equals c controlaccount

选择新的

{

MainAccount = coa.MainAccount,

Title = coa.Title,

ControlLevel = coa.ControlLevel,

ControlAccount = coa.ControlAccount,

ControlTitle = ca.Title

).FirstOrDefault();


尝试使用此示例..



列表< employee> emp =  new 列表< employee>(); 
emp.Add( new Employee()
{
EmpID = 1
EmpName = a
City = Pune
ManagerID = 11
});

emp.Add( new Employee()
{
EmpID = 2
EmpName = b
City = mumbai
ManagerID = 12
});

emp.Add( new Employee()
{
EmpID = 3
EmpName = c
City = Pune
ManagerID = 2
});

emp.Add( new Employee()
{
EmpID = 4
EmpName = d
City = 德里
ManagerID = 14
});

// ---- Linq查询

var q =(来自员工 emp
join employee2 emp中,employee.EmpID等于employee2.ManagerID
选择员工).FirstOrDefault();
< / 员工 > < / 员工 >


How do we write self join in LINQ? I have tried this:

from coa in GL0_ChartOfAccounts
  join c in GL0_ChartOfAccounts on coa.MainAccount equals c.ControlAccount into cs 
  from ca in cs.DefaultIfEmpty()
  select new 
  {
  	MainAccount = coa.MainAccount, 
	Title = coa.Title, 
	ControlLevel = coa.ControlLevel, 
	ControlAccount = coa.ControlAccount, 
	ControlTitle = ca.Title
	}

Unfortunately, this gives an anonymous type error.

To clarify this, suppose there are three columns:

MainAccount 
Title
ParentAccount

Now I want to get title or name of account instead of ParentAccount(int) how should I write this in LINQ?

解决方案

var q = (from coa in Glochartofaccount
join c in Glochartofaccount on coamainaccount equals c controlaccount
select new
{
MainAccount = coa.MainAccount,
Title = coa.Title,
ControlLevel = coa.ControlLevel,
ControlAccount = coa.ControlAccount,
ControlTitle = ca.Title
).FirstOrDefault();


try by using this sample..

List<employee> emp = new List<employee>();
emp.Add(new Employee()
{
    EmpID = 1,
    EmpName = "a",
    City = "Pune",
    ManagerID = 11
});

emp.Add(new Employee()
{
    EmpID = 2,
    EmpName = "b",
    City = "mumbai",
    ManagerID = 12
});

emp.Add(new Employee()
{
    EmpID = 3,
    EmpName = "c",
    City = "Pune",
    ManagerID = 2
});

emp.Add(new Employee()
{
    EmpID = 4,
    EmpName = "d",
    City = "Delhi",
    ManagerID = 14
});

//----Linq query

var q = (from employee in emp
        join employee2 in emp on employee.EmpID equals employee2.ManagerID
        select employee).FirstOrDefault();
</employee></employee>


这篇关于如何在LINQ中编写自联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆