简体   繁体   English

linq 中的内部连接到实体

[英]inner join in linq to entities

I have entity called Customer and it has three properties:我有一个名为 Customer 的实体,它具有三个属性:

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:我还有一个名为 Splitting 的实体,它具有三个属性:

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.现在我需要编写一个获取 companyId 和 customerId 的方法。 The method should return list of splitting that relates to the specific customerId in the companyId.该方法应返回与 companyId 中的特定 customerId 相关的拆分列表。 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 :使用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);

You can find a whole bunch of Linq examples in visual studio.您可以在 Visual Studio 中找到一大堆 Linq 示例。 Just select Help -> Samples , and then unzip the Linq samples.只需 select Help -> Samples ,然后解压缩 Linq 示例。

Open the linq samples solution and open the LinqSamples.cs of the SampleQueries project.打开 linq 示例解决方案并打开SampleQueries项目的LinqSamples.cs

The answer you are looking for is in method Linq14:您正在寻找的答案在方法 Linq14 中:

int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
int[] numbersB = { 1, 3, 5, 7, 8 };

var pairs =
   from a in numbersA
   from b in numbersB
   where a < b
   select new {a, b};

Not 100% sure about the relationship between these two entities but here goes:不是 100% 确定这两个实体之间的关系,但这里有:

IList<Splitting> res = (from s in [data source]
                        where s.Customer.CompanyID == [companyID] &&
                              s.CustomerID == [customerID]
                        select s).ToList();

IList<Splitting> res = [data source].Splittings.Where(
                           x => x.Customer.CompanyID == [companyID] &&
                                x.CustomerID == [customerID]).ToList();
public IList<Splitting> get(Guid companyId, long customrId) {    
    var res=from c in Customers_data_source
            where c.CustomerId = customrId && c.CompanyID == companyId
            from s in Splittings_data_srouce
            where s.CustomerID = c.CustomerID
            select s;

    return res.ToList();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM