LMGONCA
asked on
How to refer to a LINQ field with a variable?
I've a LINQ query working fine, then if I want to refer to a returned field from that query in LINQ I use something like:
MyObject.FieldName
And that also works fine.
Now: what I need is a method where I can use:
MyObject.%variable%.
Imagine I want to implement the following:
string GetFieldValue(string MyFieldName)
{
return MyLINQQuery.MyFieldName.To String(); //where MyLINQQuery is a public LINQ query object and MyFieldName will be a variable specified in the method.
}
How can I do this?
MyObject.FieldName
And that also works fine.
Now: what I need is a method where I can use:
MyObject.%variable%.
Imagine I want to implement the following:
string GetFieldValue(string MyFieldName)
{
return MyLINQQuery.MyFieldName.To
}
How can I do this?
ASKER
I don't know the field names....if that was the case, I could use the normal properties names.....
MyObject.FieldName = "xxx";
Basically I am calling that method from a dinamically created method...
I need something that abstract the fieldnames.
MyObject.FieldName = "xxx";
Basically I am calling that method from a dinamically created method...
I need something that abstract the fieldnames.
Forgive me if I am confused. In your code above, you give an example of something like:
MyObject.FieldName
Doesn't this mean that you know what "MyObject" is, and by extension, what its public properties are?
Can you help me understand?
MyObject.FieldName
Doesn't this mean that you know what "MyObject" is, and by extension, what its public properties are?
Can you help me understand?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have tried your great example, but I am still facing a problem :(
In the line:
public IEnumerable<Entity> MyLINQQuery;
I got the following error:
Compiler Error Message: CS0308: The non-generic type 'System.Collections.IEnume rable' cannot be used with type arguments
I have added:
using System.Linq.Dynamic;
using System.Collections;
Do you know why? Thanks.
In the line:
public IEnumerable<Entity> MyLINQQuery;
I got the following error:
Compiler Error Message: CS0308: The non-generic type 'System.Collections.IEnume
I have added:
using System.Linq.Dynamic;
using System.Collections;
Do you know why? Thanks.
ASKER
I found out why the error was:
I need to use:
using System.Collections.Generic ;
Instead of:
using System.Collections;
Working now!
Great - thanks!
I need to use:
using System.Collections.Generic
Instead of:
using System.Collections;
Working now!
Great - thanks!
ASKER
Meantime I saw I cannot use any more the MyLINQQuery object the following way:
MyLiNQQuery.Fieldname = "XXX";
Do you know why? Isn't there a way to keep both access methods?.....
MyLiNQQuery.Fieldname = "XXX";
Do you know why? Isn't there a way to keep both access methods?.....
Hi LMGONCA;
I am glad you got it working. On the other matter about not being able to use this any more:
MyLiNQQuery.Fieldname = "XXX";
I do not see why not can you post the code so that I can follow the logic.
Fernando
I am glad you got it working. On the other matter about not being able to use this any more:
MyLiNQQuery.Fieldname = "XXX";
I do not see why not can you post the code so that I can follow the logic.
Fernando
ASKER
Is quite a big class now, but let's see if I can the involved extracts of it:
public class My_Entity
{
MyDbDataContext MyDb = new MyDbDataContext();
public Entity LQEntity = new Entity(); //here now I could change to: public public IEnumerable<Entity> LQEntity....
//lots of code here
string GetFieldValue(string MyFieldName)
{
// Select the single filed in the variable MyFieldName from the query
var fieldQuery = LQEntity.AsQueryable().Sel ect(MyFiel dName);
// Used to return the value
String retField = "";
// Enumerate the single record with the single field returned
foreach (var f in fieldQuery)
{
retField = f.ToString();
}
// Return the field value.
return retField;
}
}
************************** ********** ********** *****
That was the My_Entity class
Now I consume this class like this:
My_Entity MyEntity = new My_Entity();
MyEntity.LQEntity.Code = MyValue; //here I do access the LQEntity with that object.fieldname method - and I do this in many many places...(and here is great to have the field name appearing automatically to you as you type)
Now in some places I've a datatable with the same field / column structure than the LQEntity entity and to avoid do something like this:
MyTable.Column["ColumnName "] = MyEntity.LQEntity.FieldNam e;
....dozens of fields....
That's why I want that possibility to use the LQEntity as a variable name, something like:
for each (DataColumn MyCol in MyTable.Columns)
{
MyCol[MyCol.ColumnName] = MyEntity.GetFieldValue(MyC ol.ColumnN ame);
}
Got it? :)
public class My_Entity
{
MyDbDataContext MyDb = new MyDbDataContext();
public Entity LQEntity = new Entity(); //here now I could change to: public public IEnumerable<Entity> LQEntity....
//lots of code here
string GetFieldValue(string MyFieldName)
{
// Select the single filed in the variable MyFieldName from the query
var fieldQuery = LQEntity.AsQueryable().Sel
// Used to return the value
String retField = "";
// Enumerate the single record with the single field returned
foreach (var f in fieldQuery)
{
retField = f.ToString();
}
// Return the field value.
return retField;
}
}
**************************
That was the My_Entity class
Now I consume this class like this:
My_Entity MyEntity = new My_Entity();
MyEntity.LQEntity.Code = MyValue; //here I do access the LQEntity with that object.fieldname method - and I do this in many many places...(and here is great to have the field name appearing automatically to you as you type)
Now in some places I've a datatable with the same field / column structure than the LQEntity entity and to avoid do something like this:
MyTable.Column["ColumnName
....dozens of fields....
That's why I want that possibility to use the LQEntity as a variable name, something like:
for each (DataColumn MyCol in MyTable.Columns)
{
MyCol[MyCol.ColumnName] = MyEntity.GetFieldValue(MyC
}
Got it? :)
Hi LMGONCA;
Looking at this statement, MyLiNQQuery.Fieldname, If Fieldname is the string value that holds the name of the column you wish to modify then that will not work. About the only way to make something like that is to us Reflections, this is a subject I am not too well versed on.
Fernando
Looking at this statement, MyLiNQQuery.Fieldname, If Fieldname is the string value that holds the name of the column you wish to modify then that will not work. About the only way to make something like that is to us Reflections, this is a subject I am not too well versed on.
Fernando
The statements below assume that the return from MyLINQQuery is a MyObject.
It's not exactly a pretty solution, but it would work. The deal is, you are needing to map fieldnames to (compiled) property names. This situation would come up in any similar scenario - not just with using LINQ. I suppose another option would be to use reflection...it would be more flexible...but seems overkill.
Now, regardless of how you do it, there is a bit of an inefficiency here, in that you are retrieving the whole entity/object from the datasource (I assume it's probably a db), and then only selecting one field from it.
That said, you could create a custom query that did a similar switch statement, but did a custom Select() expression based on the field name. This would lower the payload coming back from your datasource - as the only thing returned would be the string.
Let me know if this helps, or if you need more info.
Open in new window