0

Im currently building a web api 2 API using Entity Framework 6. All was going well as a lot of the api calls are just straight through to a single table and return the object as a JSON.

Now my headaches have come with trying to build a single object from various models. I am using Code first.

A Simplified version of the object i want to return is this

{ Name : Brand.Name, CategoryScores : [{CategoryId : 'x', Score : 'x'}]}

These are my current models

[Table("Brands")]
public class BrandModel
{

    [Key]
    public string BrandId { get; set; }
    public string BrandName { get; set; }
}

[Table("CategoryScores")]
public class CategoryScoresModel
{

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
    public string BrandId { get; set; }
    public string TraqCategoryScore { get; set; }}

public class TraqIndexDTO
{
    public string BrandName { get; set; }
    public IEnumerable<CategoryScoresDTO> CategoryScores { get; set; }

}

public class CategoryScoresDTO
{
    public string CategoryId { get; set; }
    public string TraqCategoryScore { get; set; }
}

Controller the logic in here will be moved out once its working

private AuthContext db = new AuthContext();

public IHttpActionResult Get()
    {
        //var results = this.repository.SelectAll();
        return Ok(GetTraqIndex());
    }
public IEnumerable<TraqIndexDTO> GetTraqIndex()
    {
        var traqIndex = from b in db.Brand
                        select new TraqIndexDTO()
                        {
                            BrandName = b.BrandName,
                            CategoryScores = getCatgoryScoresByBrandId(b.BrandId)
                        };

        return traqIndex;
    }

public IEnumerable<CategoryScoresModelDTO>getCatgoryScoresByBrandId(string brandId)
    {
        var scores = from s in db.CategoryScoresModel
                     where s.BrandId == brandId
                     select new CategoryScoresDTO() {
                         CategoryId = s.CategoryId,
                         TraqCategoryScore = s.TraqCategoryScore
                     };

        return scores;
    }

Currently I am getting this error " getCategoryScoresByBrandId(System.String)' method, and this method cannot be translated into a store expression.""

Any help and guidance or a good example to follow would be much appreciated

Thanks Rob

1 Answer 1

1

Modify GetTraqIndex:

public IEnumerable<TraqIndexDTO> GetTraqIndex()
{
    var traqIndex = db.Brand.ToList().Select(b => new TraqIndexDTO()
                    {
                        BrandName = b.BrandName,
                        CategoryScores = getCatgoryScoresByBrandId(b.BrandId).ToList()
                    }).ToList();

    return traqIndex;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks but BrandName and Category scores now do not exist in the current context
Thats ok much appreciate the help but now i get this getCatgoryScoresByBrandId(b.BrandId) as not available in the current context
Sorry my mistake type ;)
Thank you so much all working, that drove me nearly over the edge.... So the key was ToList ;)
@RobPaddock Accept answer then =)

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.