linq到实体的内部联接

Nao*_*aor 28 .net c# linq-to-entities

我有一个名为Customer的实体,它有三个属性:

public class Customer {
    public virtual Guid CompanyId;
    public virtual long Id;
    public virtual string Name;
}
Run Code Online (Sandbox Code Playgroud)

我还有一个名为Splitting的实体,它有三个属性:

public class Splitting {
    public virtual long CustomerId;
    public virtual long Id;
    public virtual string Name;
}
Run Code Online (Sandbox Code Playgroud)

现在我需要编写一个获取companyId和customerId的方法.该方法应返回与companyId中特定customerId相关的拆分列表.像这样的东西:

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

    return res.ToList();
}
Run Code Online (Sandbox Code Playgroud)

man*_*nji 80

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;
Run Code Online (Sandbox Code Playgroud)

使用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);
Run Code Online (Sandbox Code Playgroud)

  • 如果你的意思是linq扩展方法(`Select`,`Join` ...看看我的编辑) (2认同)

Rog*_*sem 6

您可以在visual studio中找到一大堆Linq示例.只需选择Help -> Samples,然后解压缩Linq样本.

打开LINQ样品溶液,然后打开LinqSamples.cs中的SampleQueries项目.

您正在寻找的答案是方法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};
Run Code Online (Sandbox Code Playgroud)