簡體   English   中英

linq 中的內部連接到實體

[英]inner join in linq to entities

我有一個名為 Customer 的實體,它具有三個屬性:

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

我還有一個名為 Splitting 的實體,它具有三個屬性:

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

現在我需要編寫一個獲取 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();
}
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;

使用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);

您可以在 Visual Studio 中找到一大堆 Linq 示例。 只需 select Help -> Samples ,然后解壓縮 Linq 示例。

打開 linq 示例解決方案並打開SampleQueries項目的LinqSamples.cs

您正在尋找的答案在方法 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};

不是 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