Search code examples
c#vb.netlinq

Using Lookup in Linq


Need to fetch the first record from Lookup when a condition is satisfied. In below, resId will be key having one or more lists value. In vb, used the below code to fetch the record having resId and record satisfying the below condition. It works perfect.. how can I use the same logic with C#

Lookup<Guid, Class> responseLookup;

result = responseLookup(Guid).FirstOrDefault(
                    Function(x) catId.Equals(x.catCode)

Tried to convert but was receiving "Method name expected" error


Solution

  • Assuming Guid in responseLookup(Guid) is some Guid value, not the type name, the following should work (I suggest using standard naming conventions and refrain from using BCL type names as variable names):

    Guid guid = ...;
    var result = responseLookup[guid]
        .FirstOrDefault(x => catId.Equals(x.catCode));