0

The title shows me the an error i.e. Count cannot be used like a method. This error shows in runtime in controller side.

Here is my code,

var dataList = (dynamic)null;

// Search    
if (!string.IsNullOrEmpty(searchValue))
{
    dataList = (from x in query
                select new
                       {
                            PartName = _Db.Part.Where(z => z.Id == x.Select(p => p.PartId).FirstOrDefault()).Select(p => p.Name),
                            ManufacturerName = _Db.Manufacture.Where(z => z.Id == x.Select(p => p.ManufacturerId).FirstOrDefault()).Select(p => p.name),
                            CategoryName = _Db.Category.Where(z => z.Id == x.Select(p => p.CategoryId).FirstOrDefault()).Select(p => p.Name == searchValue.ToString()),
                            Pcs = x.Sum(o => o.Pcs) - (from m in _Db.MaterialRecord
                                                       join s in _Db.ServiceJob on m.ServiceJobId equals s.Id
                                                       where m.pid == x.Select(p => p.PartId).FirstOrDefault()
                                                       select m).Sum(z => z.Qty),
                            Weight = _Db.Purchase.Where(p => p.Weight == x.Select(s => s.Weight).FirstOrDefault()).Select(a => a.Weight).FirstOrDefault(),
                            WeightType = x.Select(p => p.WeightTypeId).FirstOrDefault() > 0 ? ((WeightType)x.Select(p => p.WeightTypeId).FirstOrDefault()).ToString() : "",
                        }).ToList();
}
else
{
    dataList = (from x in query
        select new
        {
            PartName = _Db.Part.Where(z => z.Id == x.Select(p => p.PartId).FirstOrDefault()).Select(p => p.Name),
            ManufacturerName = _Db.Manufacture.Where(z => z.Id == x.Select(p => p.ManufacturerId).FirstOrDefault()).Select(p => p.name),
            CategoryName = _Db.Category.Where(z => z.Id == x.Select(p => p.CategoryId).FirstOrDefault()).Select(p => p.Name),
            Pcs = x.Sum(o => o.Pcs) -
            (from m in _Db.MaterialRecord
                join s in _Db.ServiceJob on m.ServiceJobId equals s.Id
                where m.pid == x.Select(p => p.PartId).FirstOrDefault()
                select m).Sum(z => z.Qty),
            Weight = _Db.Purchase.Where(p => p.Weight == x.Select(s => s.Weight).FirstOrDefault()).Select(a => a.Weight).FirstOrDefault(),

            WeightType = x.Select(p => p.WeightTypeId).FirstOrDefault() > 0 ? ((WeightType)x.Select(p => p.WeightTypeId).FirstOrDefault()).ToString() : "",
        }).ToList();
    }

    // total number of rows count     
    recordsTotal = dataList.Count();

    // Paging     
    var data = dataList.Skip(skip).Take(pageSize).ToList();

    // Returning Json Data
    return Json(new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = data });
}

Now when I run the program at that time

recordsTotal = dataList.Count();

in this line shows this error:

Non-invocable member 'System.Collections.Generic.List<<>f__AnonymousType15,System.Linq.IQueryable,System.Linq.IQueryable,int,int,string>>.Count' cannot be used like a method.

9
  • 3
    Have you tried just recordsTotal = dataList.Count; ? Commented Jan 18, 2019 at 12:55
  • The error is kinda self explanatory. Like Fildor mentioned, just remove the "()" after the count. Commented Jan 18, 2019 at 12:59
  • 1
    var dataList = (dynamic)null; I don't think this is right in your situation. Instead of handling value of searchValue in one line, inside CategoryName, you've duplicated whole code and went to casting your list to dynamic. Commented Jan 18, 2019 at 13:15
  • @SeM yah you right. Can you suggest me how I maintain code just for the search by my code. Please. Commented Jan 18, 2019 at 13:21
  • @SeM I have tried to do the different type of code but it shows the different error and that question I have shown here i.e. stackoverflow.com/questions/54252957/… but it didnt work thats why I am doing this type of code Commented Jan 18, 2019 at 13:24

1 Answer 1

2

You can't call Count() method on dynamic type which is List. Count() method is extension to IEnumerable<>. You can use Enumerable.Count() method instead:

recordsTotal = Enumerable.Count(dataList);

or remove parenthesis:

// There is Count property in List<T>
recordsTotal = dataList.Count;

More details in this post.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.