1

I can create a dynamic query but as it stands now the column needs to be typed in. I would prefer to allow selection from a combobox.

I can get the tables with this

using (var dbContext = new SalesEntities())
{
    var metadata = ((IObjectContextAdapter)dbContext).ObjectContext.MetadataWorkspace;

    var tables = metadata.GetItemCollection(DataSpace.SSpace)
                .GetItems<EntityContainer>()
                .Single()
                .BaseEntitySets;

    foreach (var table in tables)
    {
        cboTable.Items.Add(table.Name );
    }
}

And I eventually run the query with this (It would be nice if there was a way I can generalize this code so it is not reproduced for each table)

List<Sales_Regions> srsl;

var query1 = SC.Sales_Regions.Where(txtField.Text + " = @0",  txtValue.Text);
srsl = query1.ToList();
dataGridView1.DataSource = srsl;

Looking at this post (Entity Framework - how do I get the columns?) I tried this:

IEnumerable<FieldList> properties = from p in typeof(T).GetProperties()
    where (from a in p.GetCustomAttributes(false)
        where a is EdmScalarPropertyAttribute
        select true).FirstOrDefault()
    select new FieldList
    {
        FieldName = p.Name,
        FieldType = p.PropertyType.ToString (),
        FieldPK = p.GetCustomAttributes(false).Where(a => a is EdmScalarPropertyAttribute && ((EdmScalarPropertyAttribute)a).EntityKeyProperty).Count() > 0
    };

And I created a class to go with it (don't know if this is wrong or not)

class FieldList
{
    public string FieldName
    {
        get;
        set;
    }
    public string FieldType
    {
        get;
        set;
    }
    public bool FieldPK
    {
        get;
        set;
    }
}

But I get an error

The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)

I tried several things but the fact of the matter is I do not know what goes there.

This one may actually be better, I got the code to work, but I don't know how to get the data out of cols.

var cols = from meta in objctx.MetadataWorkspace.GetItems(DataSpace.CSpace)
        .Where(m => m.BuiltInTypeKind == BuiltInTypeKind.EntityType)
    from p in (meta as EntityType).Properties
        .Where(p => p.DeclaringType.Name == cboTable.SelectedItem.ToString())
    select new
    {
        PropertyName = p.Name,
        TypeUsageName = p.TypeUsage.EdmType.Name, //type name
        Documentation = p.Documentation != null ? p.Documentation.LongDescription : null //if primary key
    };

It seems like I'm really close. Trying to be as dynamic as possible.

Community
  • 1
  • 1
John
  • 267
  • 1
  • 3
  • 12
  • I don't see the point of your `FieldList` class. What is wrong with using the `PropertyInfo` class? It seems to have everything you'd need. – Mr Anderson Aug 29 '16 at 20:48
  • @Mr. Anderson, any clues on how to get what I need out of the PropertyInfo class? – John Aug 29 '16 at 21:23
  • Is it a compilation error? You probably copy pasted a generic function body. Instead of typeof(T) insert typeof(Sales_Regions). – bubi Aug 30 '16 at 18:27
  • @bubi, well if I have to hard code Sales_Regions that will make it useless. It needs to be dynamic. So it looks like the third option is the only way for me since it accepts a variable (cboTable.SelectedItem). But I can not figure out how to get the data out of cols. Perhaps I should make a new question? – John Aug 31 '16 at 12:38

0 Answers0