views:

24

answers:

2

I have two tables in my entity framework, objects, and parameters which have a foreign key pointing to the object to which they belong. I want to populate a tree with all the attributes of a certain object. So in order to find those I want to do this:

   String parentObject = "ParentObjectName";
   var getAttributes = (from o in myDB.ATTRIBUTE
                        where o.PARENT_OBJECT == parentObject
                        select o);

However when I try to do this I get an error saying it cannot convert from type OBJECT to string, even though in the database this value is stored as a string. I have a workaround where I get an instance of the parentObject, then go through every attribute and check whether it's parent_object == parentObjectInstance, but this is much less efficient than just doing 1 query. Any help would be greatly appreciate, thanks!

A: 

Try this:

String parentObject = "ParentObjectName"; 
   var getAttributes = (from o in myDB.ATTRIBUTE 
                        where o.PARENT_OBJECT.ToString() == parentObject 
                        select o); 
morganpdx
You could also try this: 'myDB.Attributes.Select(a => a.PARENT_OBJECT.ToString() == parentObject);'
morganpdx
I tried the .toString() which throws: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.
Zack
Both of those statements throw the same exception =/
Zack
Hmm. To clarify, you have a one to many table relationship between the object table and the attributes table, correct? So parent_object is a foreign key?
morganpdx
Check this thread out, I think it might have your solution. http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/19a33909-c532-419b-a533-57e431c0b30b
morganpdx
Yes, exactly. Sorry probably should've named the string parentObjectName or something to make it more clear. But it is 1 to many between those two tables, with PARENT_OBJECT being a foreign key in the attribute table.
Zack
+1  A: 

Well, PARENT_OBJECT.ToString() can't be called (implicitly or explicitly) in L2E, but if it just returns a property, you can look at that directly:

String parentObject = "ParentObjectName";
var getAttributes = (from o in myDB.ATTRIBUTE
                     where o.PARENT_OBJECT.NAME == parentObject
                     select o);

...note the .NAME

Craig Stuntz
This did the trick! Thanks a lot
Zack