3

I am trying to execute a query inside a controller using EF7 and to display data in a table.

Here are my models:

public class Resources
{
    public int id { get; set; }
    public string name { get; set; }
    public string descr { get; set; }
    public string resourcePath { get; set; }
    public string method { get; set; }
    public string format { get; set; }

    public virtual ICollection<MeasureTags> Tags { get; set; }
}

public class MeasureSets
{
    public int id { get; set; }
    public string name { get; set; }
    public string descr { get; set; }
    public string userName { get; set; }
    public bool isPublic { get; set; }

    public virtual ICollection<MeasureTags> Tags { get; set; }
}

public class MeasureTags
{
    public int id { get; set; }
    public string name { get; set; }
    public string userName { get; set; }
    public bool isPublic { get; set; }
    public int sortOrder { get; set; }

    [ForeignKey("MeasureSets")]
    public int msId { get; set; }
    public virtual MeasureSets ms { get; set; }

    public virtual Resources res { get; set; }
}

and here is the join query in the controller:

//get users measure sets
var m_sets = from t in _context.MeasureTags
             join s in _context.MeasureSets on t.ms.id equals s.id 
             where s.userName == User.Identity.Name
             select new {
                 id = t.id,
                 set = s.name,
                 tag = t.name,
                 res = t.res.name
             };

return View(m_sets.ToArray());

In the view I have a foreach loop that is able to extract correctly each row but not the columns. Error: 'object' does not contain a definition for 'id' (item.id)

<tbody>
@foreach (var item in Model)
{
   string tagid = "tag" + item.id;
       <tr>
        <td><input type="checkbox" checked id="@tagid"/></td>
        <td>@item.set</td>
        <td>@item.tag</td>
        <td>@item.res</td>
       </tr>
    }
}
</tbody>

I added also an extra model for the result but still without success. Error is 'ManageTagTableModel' does not contain a public definition for 'GetEnumerator'

public class TagTable
{
    public int id { get; set; }
    public string set { get; set; }
    public string tag { get; set; }
    public string res { get; set; }
}

public class ManageTagTableModel : IEnumerable
{
    public IEnumerator GetEnumerator()
    {
        return ((IEnumerable)TagTable).GetEnumerator();
    }
}

The question is: how can I access the 'new' generic object properties if possible without the need for additional models?

2
  • duplicate of stackoverflow.com/a/5670899/1808494 Commented Feb 11, 2016 at 1:01
  • Thanks to Stephen Muecke I was able to make this run. But I am still wandering if there is an option to acces properties of a generic object without a model.. Commented Feb 11, 2016 at 7:30

1 Answer 1

3

You need to project your query into new instances of TagTable

var m_sets = from t in _context.MeasureTags
             join s in _context.MeasureSets on t.ms.id equals s.id 
             where s.userName == User.Identity.Name
             select new TagTable {
                 id = t.id,
                 set = s.name,
                 tag = t.name,
                 res = t.res.name
             };
return View(m_sets.ToArray());

and then in the view

@model IEnumerable<TagTable>
@foreach (var item in Model)
{
    <tr>
        <td>@item.set</td>
        ....
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.