簡體   English   中英

C#LINQ選擇很多

[英]C# LINQ Select Many

我將案例與控件匹配,基本上是案例列表中的記錄,需要具有字符串m_ctrlno指定的匹配數。

到目前為止我有兩個列表,where子句是正確的,但是我不確定如何使用SelectMany來獲得匹配1個Case的3個控件。 我決定使用.Take()函數,但它似乎沒有工作。 當我在var查詢上循環時,我沒有得到3個不同控件的相同情況。

這是代碼:

List<CaseSelection> CurrentCaseList = new List<CaseSelection>();
foreach (CaseSelection CurrentCase in m_casesarraylist)
CurrentCaseList.Add(CurrentCase);

List<ControlSelection> CurrentControlList = new List<ControlSelection>();
foreach (ControlSelection CurrentControlRec in ControlList)
CurrentControlList.Add(CurrentControlRec);


var query = CurrentCaseList.SelectMany(
c => CurrentControlList.Where(o => o.pracid == c.pracid && o.sex == c.sex &&
CaseSelectionList.AgeIsInRange(c.yob, o.yob, m_years)),
(c, o) =>
new { o, c }).Take(m_ctrlno);

在您的代碼中您定義了2個列表CurrentCaseList,CurrentControlList但未定義CaseSelectionList

要獲得匹配一個案例的3個控件,請參閱下面的代碼:

SelectMany方法選擇TotalDue小於500.00的所有訂單。

這是代碼:

decimal totalDue = 500.00M;
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    ObjectSet<Contact> contacts = context.Contacts;
    ObjectSet<SalesOrderHeader> orders = context.SalesOrderHeaders;

    var query =
    contacts.SelectMany(
        contact => orders.Where(order =>
            (contact.ContactID == order.Contact.ContactID)
                && order.TotalDue < totalDue)
            .Select(order => new
            {
                ContactID = contact.ContactID,
                LastName = contact.LastName,
                FirstName = contact.FirstName,
                OrderID = order.SalesOrderID,
                Total = order.TotalDue
            }));

    foreach (var smallOrder in query)
    {
        Console.WriteLine("Contact ID: {0} Name: {1}, {2} Order ID: {3} Total Due: ${4} ",
            smallOrder.ContactID, smallOrder.LastName, smallOrder.FirstName,
            smallOrder.OrderID, smallOrder.Total);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM