C# Automapper -> Conditional Mapping - c#

I am trying to do some conditional mapping, and all the documentation and questions I have read through doesn't seem to cover this particular conditional. I was hoping someone here would have the experience or know how on the best approach to this.
I am mapping an object that has two properties. However, I do not want to map EITHER property, if a specific property is of a value. To visualize this:
foreach(var object in objectB) {
If (object.propertyA == "SomeValue")
continue;
else
Mapper.Map<ObjectA>(object);
}
however, I want the AutoMapper equivalent of the above statement. So something more like:
cfg.CreateMap<ObjectB, ObjectA>()
.ForMember(dest => dest.PropertyA, m => m.Condition(source => source.PropertyA != "SomeValue"))
.ForMember(dest => dest.PropertyB, m => m.Condition(source => source.PropertyA != "SomeVAlue" ? source.PropertyB : ignore))
But the above version obviously does not work.
Thank you in advance for your assistance.

Could be achieved using conditional mapping, see Documentation(http://docs.automapper.org/en/latest/Conditional-mapping.html) for details. To cover arrays filtering case I created a custom type converter which is a little bit tricky (see http://docs.automapper.org/en/stable/Custom-type-converters.html). The updated example is below
using AutoMapper;
using System;
using System.Collections.Generic;
namespace ConsoleAppTest2
{
class Program
{
static void Main(string[] args)
{
Mapper.Initialize(cfg => {
//Using specific type converter for specific arrays
cfg.CreateMap<Foo[], FooDto[]>().ConvertUsing(new ArrayFilterTypeConverter<Foo[], FooDto[], Foo, FooDto>(
(src, dest) => (src.Age > 0)
));
cfg.CreateMap<Foo, FooDto>()
.ForMember(dest => dest.Age, opt => opt.Condition(src => (src.Age >= 0)))
.ForMember(dest => dest.CurrentAddress, opt =>
{
opt.Condition(src => (src.Age >= 0));
opt.MapFrom(src => src.Address);
});
});
var foo = new Foo() { Name = "Name", Address = "Address", Age = -1 };
var fooDTO = new FooDto();
var fooArray = new Foo[] {
new Foo() { Name = "Name1", Address = "Address1", Age = -1 },
new Foo() { Name = "Name2", Address = "Address2", Age = 1 },
new Foo() { Name = "Name3", Address = "Address3", Age = 1 }
};
var fooDTOArray = Mapper.Map<Foo[], FooDto[]>(fooArray); //get 2 elements instead of 3
Mapper.Map(foo, fooDTO);
//The result is we skipped Address and Age properties becase Age is negative
Console.ReadLine();
}
}
public class ArrayFilterTypeConverter<TSourceArray, TDestArray, TSource, TDest> : ITypeConverter<TSourceArray, TDestArray>
{
private Func<TSource, TDest, bool> filter;
public ArrayFilterTypeConverter(Func<TSource, TDest, bool> filter)
{
this.filter = filter;
}
public TDestArray Convert(TSourceArray source, TDestArray destination, ResolutionContext context)
{
var sourceArray = source as TSource[];
List<TDest> destList = new List<TDest>();
var typeMap = context.ConfigurationProvider.ResolveTypeMap(typeof(TSource), typeof(TDest));
foreach (var src in sourceArray)
{
var dest = context.Mapper.Map<TSource, TDest>(src);
if (filter(src, dest))
destList.Add(dest);
}
// Little hack to cast array to TDestArray
var result = (TDestArray)(object)destList.ToArray();
return result;
}
}
internal class FooDto
{
public string Name { get; set; }
public string CurrentAddress { get; set; }
public int Age { get; set; }
}
internal class Foo
{
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
}
}

Related

Creating a list of an object which either does or doesn't exist in another different object list - C#

So I am trying to compare two different lists that both contain differently structured objects. One is easily accessible while the other is very nested in arrays, but sadly, these are responses from API calls so it's not a structure I can change to make them easier to compare. I want to have a list of the complete structures of items found:
var foundList = new List<Structure1>();
var notFoundList = new List<Structure1>();
The way the objects are structured are as follows:
public class ObjectsA
{
public Structure1[] Structure1 {get; set;}
}
public class Structure1
{
public int id {get; set;} //1
}
And the other object looks like:
public class ObjectsB
{
public Array1[] array1{get; set}
}
public class Array1
{
public Array2[] array2{get; set;}
}
public class Array2
{
public string id {get; set;} //"0001"
}
In a list, I added all the objects that came back from the API call, so ObjectAList contains technically just 1 deserialized object response, which contains an array of objects, while ObjectBList contains a list of objects added to it via AddRange.
At first I tried to putting an Array.Exists() inside of 2 foreach() statements.
foreach (var arr1 in ObjectsBList){
foreach (var arr2 in a.Array2){
if (Array.Exists(ObjectAList.Structure1, item => item.id == Convert.ToInt32(arr2.id)) == true){
foundList.AddRange(ObjectAList.Structure1);
}
else{
notFoundList.AddRange(ObjectAList.Structure1)
}}};
This code seemed to keep looping on the "item => item.id == Convert.ToInt32(arr2.id)" part of it, so consequently, it kept going till it found its match and so the answer was always 'true', therefore just putting everything in the foundList. I know I'm probably going at this wrong. I'm just starting out C# programming and I'm having trouble wrapping my mind around some of these things and knowing what all functions exists to help with what I need, etc. Any help would be great!
You can use linq for querying the in-memory objects.
pseudo code
public class Test {
public void T()
{
var ObjectsBList = new ObjectsB();
var ObjectAList = new ObjectsA();
var foundList = new List<Structure1>();
var notFoundList = new List<Structure1>();
var bList = ObjectsBList
.array1
.SelectMany(x => x.array2)
.Select(x => Convert.ToInt32(x.id))
.Distinct()
.ToList();
if (ObjectAList.Structure1.Any(x => bList.Contains(x.id)))
{
foundList.AddRange(ObjectAList.Structure1);
}
else
{
notFoundList.AddRange(ObjectAList.Structure1);
}
}
}
More simplified version:
Introduce an ID list property
using System;
using System.Text;
using System.Collections.Generic;
using System.Collections;
public class ObjectsA
{
public Structure1[] Structure1 { get; set; }
public List<int> IDs
{
get
{
return Structure1.Select(x => x.id).Distinct().ToList();
}
}
}
public class Structure1
{
public int id { get; set; } //1
}
public class ObjectsB
{
public Array1[] array1 { get; set; }
public List<int> IDs
{
get
{
return array1
.SelectMany(x => x.array2)
.Select(x => Convert.ToInt32(x.id))
.Distinct()
.ToList();
}
}
}
public class Array1
{
public Array2[] array2 { get; set; }
}
public class Array2
{
public string id { get; set; } //"0001"
}
public class Test
{
public void T()
{
var ObjectsBList = new ObjectsB();
var ObjectAList = new ObjectsA();
var foundList = new List<Structure1>();
var notFoundList = new List<Structure1>();
if (ObjectAList.IDs.Any(x => ObjectsBList.IDs.Contains(x)))
{
foundList.AddRange(ObjectAList.Structure1);
}
else
{
notFoundList.AddRange(ObjectAList.Structure1);
}
}
}
var objA = new ObjectsA();
var objB = new ObjectsB();
var objAIds = objA.Structure1.Select(x => x.Id).Distinct();
var objBIds = objB.Array1.SelectMany(x => x.Array2).Select(x => int.Parse(x.Id)).Distinct();
var foundInBothList = objAIds.Intersect(objBIds);
var notFoundinBList = objAIds.Except(objBIds);
var inBoth = objA.Structure1.Where(x => foundInBothList.Contains(x.Id));
var notInB = objA.Structure1.Where(x => notFoundinBList.Contains(x.Id));
Starting from .NET 6
var objBIds = objB.Array1.SelectMany(x => x.Array2).Select(x => int.Parse(x.Id)).Distinct();
var foundList = objA.Structure1.IntersectBy(objBIds, x => x.Id);
var notFoundList = objA.Structure1.ExceptBy(objBIds, x => x.Id);

Automapper Complex mapping from List to Object

I have object structure as below
public class Source
{
public Connection Connection { get; set;}
}
public class Connection
{
public string Id { get; set;
public IEnumerable<Distributor> Distributors { get; set;
}
public class Distributor { get; set; }
{
public DistributorType Type {get; set;}
public string X { get; set; }
public string Y { get; set; }
}
public class Destination
{
public Distribution Distribution { get; set;
}
public class Distribution
{
public string Id { get; set;}
public string X { get; set; }
public string Y { get; set; }
}
I would like to map the Source to Destination for the property Distribution. The mapping is as below
Source.Connection.Distributors.FirstOrDefault().X => Destination.Distribution.X
Source.Connection.Distributors.FirstOrDefault().Y => Destination.Distribution.Y
Source.Connection.Id => Destination.Distribution.Id
I have tried using the Custom Resolver but no luck
public class CustomDistributorResolver : IValueResolver<Source, Destination, Distribution >
{
public Distribution Resolve(Source source, Destination destination, Distribution destMember, ResolutionContext context)
{
var result = source.Connection.Distributors.FirstOrDefault(x => x.DistributorType =="ABC");
if (result == null) return null;
return new Distribution
{
Id = source.Connection?.Id,
X = result.X,
Y = result.Y
};
}
}
Mapping Pofile
CreateMap<Source, Destination>()
.ForMember(d => d.Distribution, opt => opt.MapFrom( new CustomDistributorResolver()));
I always get Distribution value as NULL.
I am not sure what I am doing wrong here on mapping.
-Alan-
I am not very sure what you will deal with the IEnumerable. So I use linq to approach.
var id = Source.Connection.Id;
var distributions = Source.Connection.Distributors.Select(m=> new Distribution()
{
Id = id,
X = m.X,
Y = m.Y,
});
Automapper would like :
CreateMap<Source, Destination>()
.ForMember(dest => dest.Distribution.Id ,opt => opt.MapFrom(src => src.Connection.Id))
.ForMember(dest => dest.Distribution.X, opt => opt.MapFrom(src => src.Connection.Distributors.FirstOrDefault().X))
.ForMember(dest => dest.Distribution.Y, opt => opt.MapFrom(src => src.Connection.Distributors.FirstOrDefault().Y));
You can use a type converter
private class DestinationConverter : ITypeConverter<Source, Destination>
{
public Destination Convert(Source source,
Destination destination,
ResolutionContext context)
{
var result = source.Connection.Distributors.FirstOrDefault(x => x.Type == "ABC");
if (result == null) return null;
destination = new Destination();
destination.Distribution = new Distribution
{
Id = source.Connection?.Id,
X = result.X,
Y = result.Y
};
return destination;
}
}
and register the converter
CreateMap<Source, Destination>().ConvertUsing<DestinationConverter>();

Automapper configuration for grouping the data

I am having the following models
Source:
public class Opportunity
{
public Guid Id { get; set; }
public string Name { get; set; }
public Guid QuotationId { get; set; }
public int? QuotationNumber { get; set; }
public int? QuotationVersionNumber { get; set; }
}
Target:
public class OpportunityDto
{
public Guid Id { get; set; }
public string Name { get; set; }
public List<QuotationDto> Quotations { get; set; }
}
public class QuotationDto
{
public Guid Id { get; set; }
public int Number { get; set; }
public int VersionNumber { get; set; }
}
The data I would fetch from my database would be flat as the Opportunity model and my api is exposing the OpportunityDto model.
so, in my auto-mapper configuration, I have the following code:
services
.AddSingleton(new MapperConfiguration(cfg =>
{
cfg.CreateMap<OpportunityDto, Opportunity>().ReverseMap();
cfg.CreateMap<QuotationDto, Quotation>().ReverseMap();
}).CreateMapper())
what I want to achive is a list of unique opportunities and each opportunity would have a nested member which would have the list of the quotations.
how can I make the automapper perform this grouping? right now the Quotations member of the opportunityDto returned from the api is always empty.
AutoMapper Configuration
You can do something like the following:
public static void InitialiseMapper()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<IEnumerable<Opportunity>, OpportunityDto>()
.ForMember(x => x.Id, x => x.MapFrom(y => y.FirstOrDefault().Id))
.ForMember(x => x.Name, x => x.MapFrom(y => y.FirstOrDefault().Name))
.ForMember(x => x.Quotations,
x => x.MapFrom(y => Mapper.Map<IEnumerable<Opportunity>, IEnumerable<QuotationDto>>(y).ToArray()))
;
cfg.CreateMap<Opportunity, QuotationDto>()
.ForMember(x => x.Id, x => x.MapFrom(y => y.QuotationId))
.ForMember(x => x.Number, x => x.MapFrom(y => y.QuotationNumber))
.ForMember(x => x.VersionNumber, x => x.MapFrom(y => y.QuotationVersionNumber))
;
});
}
Test
Which then successfully knows how to map as demonstrated by the following test:
[TestMethod]
public void TestMethod1()
{
var oppo1Guid = Guid.NewGuid();
var opportunities = new List<Opportunity>
{
new Opportunity
{
Id = oppo1Guid,
Name = "Mikeys Oppurtunity",
QuotationId = Guid.NewGuid(),
QuotationNumber = 169,
QuotationVersionNumber = 80,
},
new Opportunity
{
Id = oppo1Guid,
Name = "Mikeys Oppurtunity",
QuotationId = Guid.NewGuid(),
QuotationNumber = 170,
QuotationVersionNumber = 20,
}
};
var dtos = Mapper.Map<IEnumerable<Opportunity>, OpportunityDto>(opportunities);
var json = JsonConvert.SerializeObject(dtos, Formatting.Indented);
Console.WriteLine(json);
}
And the output is
Test Output
{
"Id": "623c17df-f748-47a2-bc7e-35eb124dbfa3",
"Name": "Mikeys Oppurtunity",
"Quotations": [
{
"Id": "ad8b31c2-6157-4b7f-a1f2-9f8cfc1474b7",
"Number": 169,
"VersionNumber": 80
},
{
"Id": "515aa560-6a5b-47da-a214-255d1815e153",
"Number": 170,
"VersionNumber": 20
}
]
}
#Afflatus I would give you the idea that you can follow. I am assuming that you are using AspNetCore based on your services variable.
You can create an extension method like this, to later make a call on your ConfigurationServices like services.RegisterMappingsWithAutomapper():
public static IServiceCollection RegisterMappingsWithAutomapper(this IServiceCollection services)
{
var mapperConfig = AutoMapperHelper.InitializeAutoMapper();
services.AddScoped<IMapper>(provider => new Mapper(mapperConfig));
return services;
}
The InitializeAutoMapper is below:
public static class AutoMapperHelper
{
public static MapperConfiguration InitializeAutoMapper()
{
//Load here all your assemblies
var allClasses = AllClasses.FromLoadedAssemblies();
MapperConfiguration config = new MapperConfiguration(cfg =>
{
if (allClasses != null)
{
//here normally I add another Profiles that I use with reflection, marking my DTOs with an interface
cfg.AddProfile(new MappingModelsAndDtos(allClasses));
cfg.AddProfile(new MyCustomProfile());
}
});
return config;
}
}
now you need to implement the Profile, in this case MyCustomProfile
public class ModelProfile : Profile
{
public ModelProfile()
{
//put your source and destination here
CreateMap<MySource, MyDestination>()
.ConvertUsing<MySourceToMyDestination<MySource, MyDestination>>();
}
}
you need to implement MySourceToMyDestination class then.
Bellow is an example of the code of how I am using it in my projects
public class ApplicationModel2ApplicationDto : ITypeConverter<ApplicationModel, ApplicationDto>
{
public ApplicationDto Convert(ApplicationModel source, ApplicationDto destination, ResolutionContext context)
{
var mapper = context.Mapper;
try
{
destination = new ApplicationDto
{
ApplicationId = source.ApplicationId,
ApplicationName = source.ApplicationName,
Documents = mapper.Map<IEnumerable<DocumentDto>>(source.Documents),
Tags = mapper.Map<IEnumerable<TagInfoDto>>(source.TagInfos)
};
}
catch
{
return null;
}
return destination;
}
}
Hope this helps

How to use attribute to map properties

I have the following code.
public class SyncProperty : Attribute
{
public readonly string PropertyName;
public SyncProperty(string propertyName)
{
this.PropertyName = propertyName;
}
}
public class SyncContact
{
[SyncProperty("first_name")]
public string FirstName { get; set; }
[SyncProperty("last_name")]
public string LastName { get; set; }
[SyncProperty("phone")]
public string Phone { get; set; }
[SyncProperty("email")]
public string Email { get; set; }
}
I need to create an instance of my SyncContact such as
var contact = new SyncContact { FirstName="Test", LastName="Person", Phone="123-123-1234", Email="test#test.com"};
And then with that object I need to create a NameValueCollection where the object's property uses the SyncProperty's PropertyName as the Key in the collection. Then use that to make a post request to an API. So in this case I would end up with a collection like...
collection["first_name"] = "Test"
collection["last_name"] = "Person"
collection["phone"] = "123-123-1234"
collection["email"] = "test#test.com"
How can I do that?
If you want to get some type metadata, you should use Reflection. To read attribute you can use GetCustomAttribute<AttributeType>() extension for MemberInfo.
This extension method builds sync dictionary from type properties decorated with SyncPropertyAttribute (I suggest to use the dictionary instead of NameValueCollection):
public static Dictionary<string, string> ToSyncDictionary<T>(this T value)
{
var syncProperties = from p in typeof(T).GetProperties()
let name = p.GetCustomAttribute<SyncProperty>()?.PropertyName
where name != null
select new {
Name = name,
Value = p.GetValue(value)?.ToString()
};
return syncProperties.ToDictionary(p => p.Name, p => p.Value);
}
Usage:
var collection = contact.ToSyncDictionary();
Output:
{
"first_name": "Test",
"last_name": "Person",
"phone": "123-123-1234",
"email": "test#test.com"
}
Note: if you are going to use contact data in POST request, then you should consider using simple JSON serialization attributes instead of creating your own attributes. E.g. with Json.NET:
public class SyncContact
{
[JsonProperty("first_name")]
public string FirstName { get; set; }
[JsonProperty("last_name")]
public string LastName { get; set; }
[JsonProperty("phone")]
public string Phone { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
}
Then simple serialization will do the job:
string json = JsonConvert.SerializeObject(contact);
And the result will be exactly same as above.
The attributes belong to the properties of the class, so you need to get the type of the class, then find the appropriate properties and then get the custom attributes.
Something like:
var contact = new SyncContact { FirstName="Test", LastName="Person", Phone="123-123-1234", Email="test#test.com"};
var t = typeof(SyncContact);
var props = t.GetProperties().Select(p => new { p, attr = p.GetCustomAttribute<SyncProperty>() }).Where(p => p.attr != null);
var dict = props.ToDictionary(p => p.attr.PropertyName, v => v.p.GetValue(contact) );
This leaves out any properties that aren't tagged, but you can decide to handle those differently if you wanted to.
Fiddle
Assuming SyncProperty is tagged on every property, this should do the job:
var contact = new SyncContact { FirstName="Test", LastName="Person", Phone="123-123-1234", Email="test#test.com"};
var collection = contact.GetType().GetProperties()
.Select(x => new
{
x.GetCustomAttribute<SyncProperty>().PropertyName,
Value = x.GetValue(contact).ToString()
})
.ToDictionary(x => x.PropertyName, x => x.Value);
As a helper method:
public static class SynxHelper
{
public static Dictionary<string, string> Serialize<T>(T obj)
{
return typeof(T).GetProperties()
.Select(x => new
{
SyncProperty = x.GetCustomAttribute<SyncProperty>(),
Value = x.GetValue(obj)
})
.Where(x => x.SyncProperty != null)
.ToDictionary(x => x.SyncProperty.PropertyName, x => x.Value.ToString());
}
}
// usage
var collection = SynxHelper.Serialize(contact);
Here is a one line linq solution for this
(from prop in obj.GetType().GetProperties()
where prop.GetCustomAttribute<SyncProperty>() != null
select new { Key = prop.GetCustomAttribute<SyncProperty>().PropertyName, Value = prop.GetValue(obj) })
.ToDictionary(k => k.Key, v => v.Value);
BUT!!!!!!!! Don't try doing this your self. This is not optimized and slow as all reflection is.
This is just to demonstrate how bad reflection is
static void Main(string[] args)
{
var data = Enumerable.Range(0, 10000).Select(i => new SyncContact { FirstName = "Test", LastName = "Person", Phone = "123-123-1234", Email = "test#test.com" }).ToArray();
Stopwatch sw = new Stopwatch();
long m1Time = 0;
var total1 = 0;
sw.Start();
foreach (var item in data)
{
var a = ToSyncDictionary(item);
total1++;
}
sw.Stop();
m1Time = sw.ElapsedMilliseconds;
sw.Reset();
long m2Time = 0;
var total2 = 0;
sw.Start();
foreach (var item in data)
{
var a = ToSyncDictionary2(item);
total2++;
}
sw.Stop();
m2Time = sw.ElapsedMilliseconds;
Console.WriteLine($"ToSyncDictionary : {m1Time} for {total1}");
Console.WriteLine($"ToSyncDictionary2 : {m2Time} for {total2}");
Console.ReadLine();
}
public static IDictionary<string, string> ToSyncDictionary<T>(T value)
{
var syncProperties = from p in typeof(T).GetProperties()
let name = p.GetCustomAttribute<SyncProperty>()?.PropertyName
where name != null
select new
{
Name = name,
Value = p.GetValue(value)?.ToString()
};
return syncProperties.ToDictionary(p => p.Name, p => p.Value);
}
public static IDictionary<string, string> ToSyncDictionary2<T>(T value)
{
return Mapper<T>.ToSyncDictionary(value);
}
public static class Mapper<T>
{
private static readonly Func<T, IDictionary<string, string>> map;
static Mapper()
{
map = ObjectSerializer();
}
public static IDictionary<string, string> ToSyncDictionary(T value)
{
return map(value);
}
private static Func<T, IDictionary<string, string>> ObjectSerializer()
{
var type = typeof(Dictionary<string, string>);
var param = Expression.Parameter(typeof(T));
var newExp = Expression.New(type);
var addMethod = type.GetMethod(nameof(Dictionary<string, string>.Add), new Type[] { typeof(string), typeof(string) });
var toString = typeof(T).GetMethod(nameof(object.ToString));
var setData = from p in typeof(T).GetProperties()
let name = p.GetCustomAttribute<SyncProperty>()?.PropertyName
where name != null
select Expression.ElementInit(addMethod,
Expression.Constant(name),
Expression.Condition(Expression.Equal(Expression.Property(param, p), Expression.Constant(null)),
Expression.Call(Expression.Property(param, p), toString),
Expression.Constant(null,typeof(string))));
return Expression.Lambda<Func<T, IDictionary<string, string>>>(Expression.ListInit(newExp, setData), param).Compile();
}
}
On my machine I got a 10x boost in pers.
If you can use some serializer like JSON.net since you will need to change a lot of things to make it work well and you already have a tone of stuff that dose it for you.

Create predicate with nested classes with Expression

I have this :
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
}
public class City
{
public int Id { get; set; }
public string Name { get; set; }
public int ZipCode { get; set; }
}
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int? Age { get; set; }
public City City { get; set; }
public Company Company { get; set; }
}
I'd like a some case generate the predicate like this :
var result = listPerson.Where(x => x.Age == 10).ToList<>();
Or this :
var result = listPerson.Where( x => x.Company.Name == 1234).ToList();
Or this :
var result = listPerson.Where( x => x.City.ZipCode == "MyZipCode").ToList();
Or this :
var result = listPerson.Where( x => x.Company.Name == "MyCompanyName").ToList();
Then I created a "PredicateBuilder", that's work (I get the type, if nullable or not and I build the predicate) when I do this :
BuildPredicate<Person>("Age", 10); I get this : x => x.Age == 10
But I don't how manage when there is an nested property like this :
BuildPredicate<Person>("City.ZipCode", "MyZipCode");
I'd like get this : x => x.City.ZipCode == "MyZipCode"
Or this :
BuildPredicate<Person>("City.Name", "MyName");
I'd like get this : x => x.City.Name == "MyName"
Or this :
BuildPredicate<Person>("Company.Name", "MyCompanyName");
I'd like get this : x => x.Company.Name == "MyCompanyName"
(not intending to duplicate Jon - OP contacted me to provide an answer)
The following seems to work fine:
static Expression<Func<T,bool>> BuildPredicate<T>(string member, object value) {
var p = Expression.Parameter(typeof(T));
Expression body = p;
foreach (var subMember in member.Split('.')) {
body = Expression.PropertyOrField(body, subMember);
}
return Expression.Lambda<Func<T, bool>>(Expression.Equal(
body, Expression.Constant(value, body.Type)), p);
}
The only functional difference between that and Jon's answer is that it handles null slightly better, by telling Expression.Constant what the expected type is. As a demonstration of usage:
static void Main() {
var pred = BuildPredicate<Person>("City.Name", "MyCity");
var people = new[] {
new Person { City = new City { Name = "Somewhere Else"} },
new Person { City = new City { Name = "MyCity"} },
};
var person = people.AsQueryable().Single(pred);
}
You just need to split your expression by dots, and then iterate over it, using Expression.Property multiple times. Something like this:
string[] properties = path.Split('.');
var parameter = Expression.Parameter(typeof(T), "x");
var lhs = parameter;
foreach (var property in properties)
{
lhs = Expression.Property(lhs, property);
}
// I've assumed that the target is a string, given the question. If that's
// not the case, look at Marc's answer.
var rhs = Expression.Constant(targetValue, typeof(string));
var predicate = Expression.Equals(lhs, rhs);
var lambda = Expression.Lambda<Func<T, bool>>(predicate, parameter);

Categories