None
None

Reputation: 5670

From Dictionary select only specified keys using Linq

I have an object like this

object tst = folderItems.AsEnumerable().Select(i => new {
   i.File.ListItemAllFields.FieldValues,
   i.File.UniqueId
});

in this code File.ListItemAllFields.FieldValues is a dictionary. While constructing this object I want to select only few items from this dictionary. And I have tried something like this

object tst = folderItems.AsEnumerable().Select(i => new {
    i.File.ListItemAllFields.FieldValues.Where(x=>x.Key=="mykey"),
    i.File.UniqueId
});

But this resulted in a compile time error. How can I achieve this?

Error

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

Upvotes: 0

Views: 253

Answers (1)

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34150

Well it simply says that for your anonymous class you should provide a name:

object tst = folderItems.AsEnumerable().Select(i => new {
                FieldValues = i.File.ListItemAllFields.FieldValues.Where(x=>x.Key=="mykey"),
                i.File.UniqueId
            }); 

Note that if you don't give it a name (like the second property), the property name (UniqueId in this cause will be used), this you know as you have used it like that, however if it is not a property and it's a method, you should give it a name yourself, for example you'll get an error if you don't give a name for something.Count() and it should be Count = something.Count() The above code should solve your problem. also consider adding ToList(), ...

.Where(x=>x.Key=="mykey").ToList() 

Upvotes: 1

Related Questions