1

I have been trying to generate a SiteMap for multiple websites for a custom CMS that I am writing.

My situation is as follows: I have a class in an Assembly called SiteMapItem:

public abstract class SiteMapItem
{
    public abstract string Url { get; }
}

I have three separate classes in a different Assembly that extend SiteMapItem:

public class Team : SiteMapItem
{ ... }

public class Partner : SiteMapItem
{ ... }

public class Project : SiteMapItem
{ ... }

In the Assembly that is going to generate the SiteMap, I am running the following code to get a list of the Types that have IsSubClassOf(typeof(SiteMapItem)):

foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
{
    if (a.GetTypes().Any(t => t.IsSubclassOf(typeof(SiteMapItem))))
    {
        winner = a;
    }
}

Once I have Winner, I get a list of Types that have the IsSubClassOf SiteMapItem:

List<Type> types = winner.GetTypes().Where(p => p.IsSubclassOf(typeof(SiteMapItem))).ToList();

I then Iterate over types to run the db.Database.SqlQuery - which is where I am stuck (db is a DataContext):

var items = db.Database.SqlQuery(t, "SELECT * FROM @table", new SqlParameter("table", t.Name"));

The above code returns nothing, When debugging, the type is set correctly for each iteration, However when I inspect items, and try to evaluate results, I get the error Children could not be evaluated.

I would normally use the alternative:

db.Database.SqlQuery<T>("SELECT * FROM @fdsfdsa", ...) 

but because I don't have a Class (Its in another assembly) I cannot.

Any help would be appreciated.

2
  • Are you sure you're not swallowing an exception? I get Must declare the table variable "@table"., which means that the statement expects a table variable and can't handle schema tables in this way. Commented May 18, 2015 at 7:32
  • I have tried it this way and also hard coded the table name in. Makes no difference. The only exception that I am swollowing is a reflection exception about mscorlib not having a getter for some property Commented May 18, 2015 at 7:46

1 Answer 1

2

Your code

var items = db.Database.SqlQuery(t, "SELECT * FROM @table", new SqlParameter("table", t.Name"));

returns DbRawSqlQuery not rows.

you need to access by

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

1 Comment

How could I miss something so simple. Many thanks :)

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.