Count cannot be used like a method in an ASP.NET MVC controller












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.











share|improve this question




















  • 3





    Have you tried just recordsTotal = dataList.Count; ?

    – Fildor
    Jan 18 at 12:55











  • The error is kinda self explanatory. Like Fildor mentioned, just remove the "()" after the count.

    – hoek rand
    Jan 18 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.

    – SeM
    Jan 18 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 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

    – karan
    Jan 18 at 13:24
















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.











share|improve this question




















  • 3





    Have you tried just recordsTotal = dataList.Count; ?

    – Fildor
    Jan 18 at 12:55











  • The error is kinda self explanatory. Like Fildor mentioned, just remove the "()" after the count.

    – hoek rand
    Jan 18 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.

    – SeM
    Jan 18 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 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

    – karan
    Jan 18 at 13:24














0












0








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.











share|improve this question
















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.








c# model-view-controller






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 18 at 13:23









marc_s

573k12811071255




573k12811071255










asked Jan 18 at 12:50









karankaran

206112




206112








  • 3





    Have you tried just recordsTotal = dataList.Count; ?

    – Fildor
    Jan 18 at 12:55











  • The error is kinda self explanatory. Like Fildor mentioned, just remove the "()" after the count.

    – hoek rand
    Jan 18 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.

    – SeM
    Jan 18 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 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

    – karan
    Jan 18 at 13:24














  • 3





    Have you tried just recordsTotal = dataList.Count; ?

    – Fildor
    Jan 18 at 12:55











  • The error is kinda self explanatory. Like Fildor mentioned, just remove the "()" after the count.

    – hoek rand
    Jan 18 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.

    – SeM
    Jan 18 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 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

    – karan
    Jan 18 at 13:24








3




3





Have you tried just recordsTotal = dataList.Count; ?

– Fildor
Jan 18 at 12:55





Have you tried just recordsTotal = dataList.Count; ?

– Fildor
Jan 18 at 12:55













The error is kinda self explanatory. Like Fildor mentioned, just remove the "()" after the count.

– hoek rand
Jan 18 at 12:59





The error is kinda self explanatory. Like Fildor mentioned, just remove the "()" after the count.

– hoek rand
Jan 18 at 12:59




1




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.

– SeM
Jan 18 at 13:15





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.

– SeM
Jan 18 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 at 13:21







@SeM yah you right. Can you suggest me how I maintain code just for the search by my code. Please.

– karan
Jan 18 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

– karan
Jan 18 at 13:24





@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

– karan
Jan 18 at 13:24












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54254383%2fcount-cannot-be-used-like-a-method-in-an-asp-net-mvc-controller%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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.






    share|improve this answer






























      1














      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.






      share|improve this answer




























        1












        1








        1







        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.






        share|improve this answer















        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 18 at 13:11

























        answered Jan 18 at 13:06









        SeMSeM

        4,49211529




        4,49211529






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54254383%2fcount-cannot-be-used-like-a-method-in-an-asp-net-mvc-controller%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Liquibase includeAll doesn't find base path

            How to use setInterval in EJS file?

            Petrus Granier-Deferre