Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is what I want to make:

C#
output.Where(d => d.JN_NewsCategories.NewsCategoriesEn.Contains("event"));


d is a Model. my model name is JN_News

I start to write code :

C#
var modelName = "JN_News";
var subModelName = "JN_NewsCategories";
var subModelField = "NewsCategoriesEn";
var splitSeacrh="event";

Type modelClass = GetModel(modelName);
ParameterExpression peSubModel = Expression.Parameter(modelClass, "x");
//Note this line:
Expression left= Expression.Property(peSubModel, modelClass.GetProperty(subModelName));
// left= x.JN_NewsCategories
//How do I access the field.  Like this : x.JN_NewsCategories.NewsCategoriesEn
// I tried very hard but did not succeed
Expression right= Expression.Constant(splitSeacrh);
MethodCallExpression conditionExpressionSubModel = Expression.Call(left,
typeof(string).GetMethod("Contains"), right);
Posted
Comments
vikinghunter 27-Aug-14 2:15am    
Let me have a try..

1 solution

Is this you want?

C#
var modelName = "JN_News";
var subModelName = "JN_NewsCategories";
var subModelField = "NewsCategoriesEn";
var splitSeacrh = "event";

Type modelClass = GetModel(modelName);
ParameterExpression peSubModel = Expression.Parameter(modelClass, "x");

Expression left = Expression.Property(peSubModel, modelClass.GetProperty(subModelName));

left = Expression.Field(left, modelClass.GetProperty(subModelName).PropertyType.GetField(subModelField));

Expression right = Expression.Constant(splitSeacrh, typeof(string));

MethodCallExpression conditionExpressionSubModel = Expression.Call(left,

typeof(string).GetMethod("Contains", new Type[] { typeof(string) }), right);

In my imagination,your models should look like this:
C#
class JN_News
{
    public Categories JN_NewsCategories { get; set; }
}

class Categories
{
    public string NewsCategoriesEn = "eventForSomething";
}
 
Share this answer
 
v7

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900