内加入LINQ到实体 [英] inner join in linq to entities

查看:161
本文介绍了内加入LINQ到实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有实体,称为客户,它有三个属性:

 公共类客户{
    公共虚拟的Guid CompanyId;
    公共虚长的标识;
    公共虚拟字符串名称;
}
 

我还实体,称为分裂,它有三个属性:

 公共类拆分{
    公共虚长的客户ID;
    公共虚长的标识;
    公共虚拟字符串名称;
}
 

现在我需要编写得到companyId和客户ID的方法。该方法应该返回分束,其涉及特定客户ID,在companyId列表。 事情是这样的:

 公开的IList<分裂>得到(GUID companyId,长customrId){
    VAR水库从分裂S =
            从C客户
            ......如何继续?

    返回res.ToList();
}
 

解决方案

 变种水库从s =分裂
          加入c。在客户上s.CustomerId等于c.Id
         其中,c.Id == customrId
            &功放;&安培; c.CompanyId == companyId
        选择S;
 

使用扩展方法

  VAR RES = Splitting.Join(客户,
                 S => s.CustomerId,
                 C => c.Id,
                 (S,C)=>新{S,C})
           。凡(SC => sc.c.Id ==用户id&功放;&安培; sc.c.CompanyId == companId)
           。选择(SC => sc.s);
 

I have entity called Customer and it has three properties:

public class Customer {
    public virtual Guid CompanyId;
    public virtual long Id;
    public virtual string Name;
}

I have also entity called Splitting and it has three properties:

public class Splitting {
    public virtual long CustomerId;
    public virtual long Id;
    public virtual string Name;
}

Now I need to write a method that gets companyId and customerId. The method should return list of splitting that relates to the specific customerId in the companyId. Something like this:

public IList<Splitting> get(Guid companyId, long customrId) {    
    var res=from s in Splitting 
            from c in Customer 
            ...... how to continue?

    return res.ToList();
}

解决方案

var res = from s in Splitting 
          join c in Customer on s.CustomerId equals c.Id
         where c.Id == customrId
            && c.CompanyId == companyId
        select s;

Using Extension methods:

var res = Splitting.Join(Customer,
                 s => s.CustomerId,
                 c => c.Id,
                 (s, c) => new { s, c })
           .Where(sc => sc.c.Id == userId && sc.c.CompanyId == companId)
           .Select(sc => sc.s);

这篇关于内加入LINQ到实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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