I've a list with a lookup column named Agents.

I want to filter it by using the Lookup Value in "Agents" with Linq but I don't understand how to be able to compare the lookup Value to a string.

the code looks like this now:

var filteredConnectList = connectItems.Where(u => u.FieldValues["Agent"].ToString().ToLower() == user.FieldValues["NameAndMail"].ToString().ToLower()).ToList();

u.FieldValues["Agents"] returns an object of FieldLookupValue I suppose but I need to access the lookup Value in that object.

Some help would be nice here ;)


Try below formula:

var filteredConnectList = connectItems.Where(u => ((FieldLookupValue) u.FieldValues["Agent"]) == user.FieldValues["NameAndMail"].ToString().ToLower()).ToList();

Hope this will help you.

  • Unfortunate not, this is made in CSOM code so the SPFieldLookupValue does not exist and FieldLookupValue does not have a constructor thats takes arguments – Magnus Jansson Jun 27 '16 at 8:18
  • Please verify if this works, I've updated my answer. – Dikesh Gandhi Jun 27 '16 at 8:28
  • Nope. I've already tried that casting. – Magnus Jansson Jun 27 '16 at 8:40
  • Please verify if this works, I've updated my answer. May be this should work. – Dikesh Gandhi Jun 27 '16 at 12:03
  • Yes I missed that paranthesis around the whole casting.. Thank you very very much. – Magnus Jansson Jun 27 '16 at 12:24

Try below query

var filteredConnectList = connectItems.Where(u => u.FieldValues["Agent"] != null &&
            (new SPFieldLookupValueCollection(u => u.FieldValues["Agent"].ToString())).OfType<SPFieldLookupValue>()
            .Where(v => v.LookupValue.ToString().ToLower() == user.FieldValues["NameAndMail"].ToString().ToLower()).ToList();

Please let me know if any question on this.

  • Thank you for your effort, but this is still a client application so there are no SP_nnnnnn objects availible. the SPFieldLookupValueCollection for example and it seens that there are no equvalent CSOM object for that. – Magnus Jansson Jun 27 '16 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.