1

I'm trying to convert the following code to return an array result. But am unable to get it to work. I'm fairly new to the Linq framework.

Here is the Code I have:

// GETAll api/category
public IEnumerable<Category> GetAll()
{
    nopMass db = new nopMass();

    var model = db.Categories.Where(x => x.ParentCategoryId == 0);

    return model.ToArray();
}

This is what I want it to return

// GETAll api/category
public IEnumerable<Category> GetAll()
{
    return new Category[]
    {
        new Category
        {
            ParentCategoryId = 1,
            Name = "New Vehicles"
        },
        new Category
        {
            ParentCategoryId = 2,
            Name = "Used Vehicles"
        }
    };
}

When I access the first code in HTML I don't get a result displaying. The second code gives an output.

Here is Html and Jquery Code

<ul id="products" />

<script>
     var uri = 'api/category';

     $(document).ready(function () {
         // Send an AJAX request
         try
         {
             $.getJSON(uri)
                 .done(function (data) {
                     // On success, 'data' contains a list of products.
                     $.each(data, function (key, item) {
                         // Add a list item for the product.
                         $('<li>', { text: formatItem(item) }).appendTo($('#products'));
                     });
                 });
         }
         catch (e) {
             alert(e.message);
         }
});

function formatItem(item) {
    return item.Name;
}

</script>
3
  • The requested result is only an example. When I debug the line, I do see values being returned, but it's not displaying on the front end :( Commented Feb 12, 2017 at 19:59
  • 2
    If mopMass is a DBContext you should really be disposing of it when you are done with it. Commented Feb 12, 2017 at 20:00
  • It seems like the .done in JQuery is not executing Commented Feb 12, 2017 at 20:05

2 Answers 2

2

Here is Your answer refactored LINQ

// GETAll api/category
public IEnumerable<Category> GetAll() {
    using(var db = new nopMass()) {

        var cats = db.Categories
                    .Where(x => x.ParentCategoryId == 0)
                    .AsEnumerable()
                    .Select(cat => new Category { 
                        ParentCategoryId = cat.ParentCategoryId, 
                        Name = cat.Name 
                     })
                    .ToArray();

        return cats;
    }
}

And also, as mentioned in the comments, making sure the the db context is disposed of properly after use.

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

2 Comments

I get the following error with this "The entity or complex type 'WebAPI.Models.Category' cannot be constructed in a LINQ to Entities query." I think I would need to create a separate Category object that is not part of the DBContext object. This would involve more coding than my answer below?
@Orion you can add AsEnumerable, ToList or one of the other expressions that would enumerate the collection before doing the Select
1

A took some time, but I finally got it to work :)

    // GETAll api/category
    public IEnumerable<Category> GetAll()
    {
        nopMass db = new nopMass();

        var model = db.Categories.Where(x => x.ParentCategoryId == 0);

        Category[] cats = new Category[model.Count()];

        int index = 0;
        foreach (var cat in model)
        {
            cats[index] = new Category { ParentCategoryId = cat.ParentCategoryId, Name = cat.Name };
            index++;
        }

        return cats;
    }

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.