-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to map list of nested object with Smart #3
Comments
Sorry, map to nested objects are not supported now. Currently, if you prepare your own IResultMapperFactory, you can use it to handle nested objects. Smart.Data.Accessor/Data/Accessor/Mappers/ObjectResultMapperFactory.cs Each is a mapper to a standard object and a single value mapper implementation similar to ExecuteScalar. |
Oh, I made a mistake. |
Tuple map support was added in version 1.2.0. [Query]
List<ValueTuple<Company, Staff>> Query(); Use it like Dapper's Result Multi-Mapping. public class ParentEntity
{
public int Id { get; set; }
public string Name { get; set; }
public List<ChildEntity> Children { get; set; }
}
public class ChildEntity
{
public int Id { get; set; }
public string Name { get; set; }
} accessor.Query().MapOneToMany(p => p.Id, (p, cs) => p.Children = cs) // Helper
public static class NestedExtensions
{
public static IEnumerable<TP> MapOneToMany<TP, TC, TPKey>(
this IEnumerable<Tuple<TP, TC>> source,
Func<TP, TPKey> parentKeySelector,
Action<TP, List<TC>> combiner)
{
return MapOneToMany(
source,
x => x.Item1,
x => x.Item2,
parentKeySelector,
EqualityComparer<TPKey>.Default,
combiner);
}
public static IEnumerable<TP> MapOneToMany<T, TP, TC, TPKey>(
this IEnumerable<T> source,
Func<T, TP> parentSelector,
Func<T, TC> childSelector,
Func<TP, TPKey> parentKeySelector,
IEqualityComparer<TPKey> keyComparer,
Action<TP, List<TC>> combiner)
{
using (var en = source.GetEnumerator())
{
if (en.MoveNext())
{
var item = en.Current;
var parent = parentSelector(item);
var key = parentKeySelector(parent);
var children = new List<TC>
{
childSelector(item)
};
while (en.MoveNext())
{
var newItem = en.Current;
var newParent = parentSelector(newItem);
var newKey = parentKeySelector(newParent);
if (!keyComparer.Equals(key, newKey))
{
combiner(parent, children);
yield return parent;
key = newKey;
children = new List<TC>();
}
children.Add(childSelector(newItem));
}
combiner(parent, children);
yield return parent;
}
}
}
} |
Dear usausa,
I want execute the query to return and can map to object like this:
Please guide me how to implement it
Thanks
The text was updated successfully, but these errors were encountered: