gaurav bajodia
gaurav bajodia

Reputation: 1

Getting a value from list of strings

The entity instance here is getting a list of 10 arrays from which I need to get the value of invalid Dir.

var entityInstance = FacadeServiceContext.LoadEntityBaseInstances(entityName, searchParams, context: null).ToList();
if (entityInstance != null && entityInstance.Any())
{
    var invalidDirflag = entityInstance.Where(item => item.Equals(CustomConstants.ICMInvalidDIR));
    foreach (var item in entityInstance )
    {
        if( invalidDirflag.Equals(false))
         {
            var filters = new Dictionary<string, List<string>>();
            addHeaderFilters(filters, request);
            addLineItems(filters, (JArray)request[SourcingConstants.ClientSoCoApplicableDocumentsAttributesItemsFieldName]);
            coAppDocs.AddRange(filterDeterminedDocuments(entityInstance, filters));
        }
    } 
}

I get an exception with the above code:

Enumeration yielded no results

How do I do this?

Upvotes: 0

Views: 65

Answers (1)

KimMeo
KimMeo

Reputation: 320

Linq Function "Where" could return empty iteration. Check your code line 4. Plus, "Where" Function does not return bool type. I recommend to use "FirstOrDefault()" on line 4. If iteration is empty, variable invalidDirflag will be set to false. See below code.

var entityInstance = FacadeServiceContext.LoadEntityBaseInstances(entityName, searchParams, context: null).ToList();
if (entityInstance != null && entityInstance.Any())
{
    var invalidDirflag = entityInstance.Where(item => item.Equals(CustomConstants.ICMInvalidDIR)).FirstOrDefault();
    foreach (var item in entityInstance )
    {
        if( invalidDirflag.Equals(false))
         {
            var filters = new Dictionary<string, List<string>>();
            addHeaderFilters(filters, request);
            addLineItems(filters, (JArray)request[SourcingConstants.ClientSoCoApplicableDocumentsAttributesItemsFieldName]);
            coAppDocs.AddRange(filterDeterminedDocuments(entityInstance, filters));
        }
    } 
}

Upvotes: 1

Related Questions