繁体   English   中英

如何从匿名类型IEnumerable中选择项目的子集?

[英]How do I select a subset of items from an anonymous type IEnumerable?

我有以下代码。

MyDataContext db = MyDataContext.Create();
            bc =
                db.BenefitCodes.Select(
                    b =>
                    new
                        {
                            BenCd = b.BenCd
                            , Description = b.BenDesc
                            , BenInterest = b.BenInterest
                            , CodeDescription = string.Format("{0} - {1}", b.BenCd, b.BenDesc)
                        });

我不得不走匿名类型路线,因为CodeDescription不是BenefitCode的属性,客户希望它以这种方式出现在dropDrownList中。 无论如何,我的问题是如何从此列表中选择项目的子集? 我需要根据BenInterest属性选择项目。

因此,这将返回IEnumerable,所以我试图走这条路线,而这就是我遇到的问题。 我的意图是建立一个新的IEnumerable列表并为其设置下拉数据源。

 IEnumerator enumerator = BenefitCodes.GetEnumerator(); 
        while(enumerator.MoveNext())
        {
              //What can I do here to return items based on BenInterest? 
              //I basically either want items that have a BenInterest of 'E'
              // or items that DO NOT have a BenInterest of 'E'
              // this is based on the value of a radioButtonList on the page
        }

因此,如何创建一个仅包含所需项目的相同匿名类型的新Enumerable。

谢谢你的帮助。 干杯,〜ck

您可以使用:

var newCollection = bc.Where( e => e.BenInterest == 'E' );

暂无
暂无

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

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