C#LINQ选择多个 [英] C# LINQ Select Many

查看:206
本文介绍了C#LINQ选择多个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我匹配病例对照,基本都在案例列表记录,需要有字符串中指定匹配的数量 m_ctrlno

到目前为止,我有两个列表,where子句是正确的,但我不能确定如何使用的SelectMany 获得3控制1相匹配情况。我决定使用。取()功能,但它似乎并不奏效。我没有得到相同的情况下,有3种不同的控制,当我循环的变种查询。

下面是code:

 名单,其中,CaseSelection> CurrentCaseList =新的名单,其中,CaseSelection>();
的foreach(CaseSelection CurrentCase在m_casesarraylist)
CurrentCaseList.Add(CurrentCase);

名单< ControlSelection> CurrentControlList =新的名单,其中,ControlSelection>();
的foreach(ControlSelection CurrentControlRec在ControlList)
CurrentControlList.Add(CurrentControlRec);


VAR的查询= CurrentCaseList.SelectMany(
C => CurrentControlList.Where(O => o.pracid == c.pracid和放大器;&安培; o.sex == c.sex和放大器;&安培;
CaseSelectionList.AgeIsInRange(c.yob,o.yob,m_years)),
(C,O)=>
新{O,C})取(m_ctrlno)。
 

解决方案

在你的code您定义2列表 CurrentCaseList,CurrentControlList ,但没有定义 CaseSelectionList

要获得3控制匹配一个情况下,对于这种见下文code:

的SelectMany 方法来选择所有的订单,其中 TotalDue 小于500.00。

下面是code:

 十进制totalDue = 500.00M;
使用(AdventureWorksEntities上下文=新AdventureWorksEntities())
{
    对象集<联系与GT;联系人= context.Contacts;
    对象集< SalesOrderHeader的>订单= context.SalesOrderHeaders;

    VAR的查询=
    contacts.SelectMany(
        触点=> orders.Where(为了=>
            (contact.ContactID == order.Contact.ContactID)
                &功放;&安培; order.TotalDue< totalDue)
            。选择(为了=>新建
            {
                的ContactID = contact.ContactID,
                姓氏= contact.LastName,
                名字= contact.FirstName,
                订单ID = order.SalesOrderID,
                总= order.TotalDue
            }));

    的foreach(查询VAR smallOrder)
    {
        Console.WriteLine(联系人ID:{0}名称:{1},{2}订单ID:{3}总计由于:$ {4},
            smallOrder.ContactID,smallOrder.LastName,smallOrder.FirstName,
            smallOrder.OrderID,smallOrder.Total);
    }
}
 

I am matching Cases to Controls, basically records in the Case list, need to have the number of matches that is specified in the string m_ctrlno.

So far I have two lists, the where clause is correct, however I'm unsure how to use SelectMany to get the 3 Controls that match 1 Case. I decided to use the .Take() function however it doesn't seem to be working. I'm not getting the same case with 3 different controls when i cycle on the var query.

Here is the code:

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

解决方案

In your code You define 2 list CurrentCaseList,CurrentControlList but not define CaseSelectionList.

To get the 3 controls that matches one case, for this see below code:

the SelectMany method to select all orders where TotalDue is less than 500.00.

Here is the code:

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);
    }
}

这篇关于C#LINQ选择多个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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