简体   繁体   中英

Creating a sublist from a parent list

I have a collection of custom objects, dependents. The custom object has about 70 properties. I want to extract just one property, the membernumber. I have the following code, where I am extracting the membernumbers and creating another list:

var memberIDs = (from d in dependents
                            select new
                                   {
                                       d.MemberNum 
                                   });
            foreach(var id in memberIDs)
            {
                string idValue = id.ToString();
            }

The problem is that idValue comes as "{ MemberNum = 20044782604 }" instead of just the "20044782604". Please let me know how to resolve it.

Thanks

That's because you are creating a new anonymous type with the MemberNum as a property. Just select it instead.

var memberIDs = from d in dependents
                select d.MemberNum;

This will yield an IEnumerable<int> instead of an IEnumerable<AnonymousType> (assuming MemberNum is of type int).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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