user1923200
user1923200

Reputation: 59

Sequence contains no matching element. Error in '/' Application

This is my Entities Class calling:

public static Type GetJobItemType(string name)
{
    var result = _JobItemTypes.GetOrAdd(name, x => typeof(JobItemExt).Assembly.GetTypes().Single(y => y.Name == x && typeof(JobItemExt).IsAssignableFrom(y)));
    return result;
}

This is where it is called

job.Items = multi.Read().Select((p) =>
                {
                    var result = Activator.CreateInstance(JobItemExt.GetJobItemType(p.JobItemTypeName)) as JobItemExt;
                    result.Id = p.Id;
                    result.TenantId = p.TenantId;
                    result.Quantity = p.Quantity;
                    result.ParentId = p.ParentId;
                    result.PaintCost = p.PaintCost;
                    result.ModifiedDate = p.ModifiedDate;
                    result.ModifiedBy = p.ModifiedBy;
                    result.LaborCost = p.LaborCost;
                    result.JobItemTypeName = p.JobItemTypeName;
                    result.JobId = p.JobId;
                    result.DisplayText = p.DisplayText;
                    result.CreatedDate = p.CreatedDate;
                    result.CreatedBy = p.CreatedBy;
                    return result;
                }).ToList();

This is the stacktrace I am getting

Source File: d:\saha progs\CodeProject_New\dev\CuttingEdgePainting.Entities\JobItems\JobItemExt.cs Line: 89 Stack Trace: [InvalidOperationException: Sequence contains no matching element] System.Linq.Enumerable.Single(IEnumerable1 source, Func2 predicate) +2535897 CuttingEdgePainting.Entities.JobItems.JobItemExt.b__0(String x) in d:\saha progs\CodeProject_New\dev\CuttingEdgePainting.Entities\JobItems\JobItemExt.cs:89 System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory) +72 CuttingEdgePainting.Entities.JobItems.JobItemExt.GetJobItemType(String name) in d:\saha progs\CodeProject_New\dev\CuttingEdgePainting.Entities\JobItems\JobItemExt.cs:89 CuttingEdgePainting.Services.DataServices.JobServiceExt.b__2c(Object p) in d:\saha progs\CodeProject_New\dev\CuttingEdgePainting.Services\DataServices\JobServiceExt.cs:182 System.Linq.WhereSelectEnumerableIterator2.MoveNext() +145 System.Collections.Generic.List1..ctor(IEnumerable1 collection) +381 System.Linq.Enumerable.ToList(IEnumerable1 source) +58 CuttingEdgePainting.Services.DataServices.JobServiceExt.GetJob(Int32 id) in d:\saha progs\CodeProject_New\dev\CuttingEdgePainting.Services\DataServices\JobServiceExt.cs:180 CuttingEdgePainting.ExteriorNew.Controllers.Mobile.JobController.Edit(Int32 id) in d:\saha progs\CodeProject_New\dev\CuttingEdgePainting.ExteriorNew\Controllers\Model\JobController.cs:91 lambda_method(Closure , ControllerBase , Object[] ) +161 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +59 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +435 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) +60 System.Web.Mvc.Async.ActionInvocation.InvokeSynchronousActionMethod() +76 System.Web.Mvc.Async.AsyncControllerActionInvoker.b__36(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +36 System.Web.Mvc.Async.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) +73 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +136 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49 System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3c() +117 System.Web.Mvc.Async.<>c__DisplayClass45.b__3e() +323 System.Web.Mvc.Async.<>c__DisplayClass30.b__2f(IAsyncResult asyncResult) +44 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +47 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +136 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50 System.Web.Mvc.Async.<>c__DisplayClass28.b__19() +72 System.Web.Mvc.Async.<>c__DisplayClass1e.b__1b(IAsyncResult asyncResult) +185 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +42 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +133 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40 System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +70 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +139 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44 System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult, Controller controller) +39 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +62 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +139 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39 System.Web.Mvc.MvcHandler.b__4(IAsyncResult asyncResult, ProcessRequestState innerState) +39 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +70 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +139 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9629708 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

When debugged I get the error on field name c_DisplayClass17 (which I do not have in the dictionary also)

Where is it getting referenced? I am using MVC 3 Entity 4.5

Upvotes: 0

Views: 4414

Answers (1)

DavidG
DavidG

Reputation: 119066

The error you get is because you are using Enumerable.Single with an expression that doesn't match any values in the enumeration. From the documentation:

InvalidOperationException No element satisfies the condition in predicate. -or- More than one element satisfies the condition in predicate. -or- The source sequence is empty.

You can either handle that error or instead use Enumerable.SingleOrDefault which will simply return null when no matches are found.

Upvotes: 2

Related Questions