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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
karan
  • 482
  • 1
  • 8
  • 30
  • 3
    Have you tried just `recordsTotal = dataList.Count;` ? – Fildor Jan 18 '19 at 12:55
  • The error is kinda self explanatory. Like Fildor mentioned, just remove the "()" after the count. – hoek rand Jan 18 '19 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`. – SᴇM Jan 18 '19 at 13:15
  • @SeM yah you right. Can you suggest me how I maintain code just for the search by my code. Please. – karan Jan 18 '19 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. **https://stackoverflow.com/questions/54252957/how-to-get-value-from-microsoft-entityframeworkcore-query-internal-entityqueryab** but it didnt work thats why I am doing this type of code – karan Jan 18 '19 at 13:24
  • @karan I don't think your code will work correctly if you resolve `Count` problem. There is a issue with `.Select(p => p.Name == searchValue.ToString()),`, which will return a collection of `bool`. – SᴇM Jan 18 '19 at 13:25
  • @SeM yes you are right for that I have change by **CategoryName = _Db.Category.Where(z => z.Id == x.Select(p => p.CategoryId).FirstOrDefault()).Select(p => p.Name.Contains(searchValue.ToString())),** but showing the true, false value in the place of string value – karan Jan 18 '19 at 13:28
  • @karan It will also return collection of `bool`s. If you need to filter, use `Where` then `Select`. – SᴇM Jan 18 '19 at 13:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/186925/discussion-between-karan-and-sem). – karan Jan 18 '19 at 13:32

1 Answers1

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.

SᴇM
  • 7,024
  • 3
  • 24
  • 41