Accessing an extension method on a dbset subtype - methods

I have an extension method defined as:
public static class CurrentItemExtensions
{
static GPOPricingEntities ctx = new GPOPricingEntities();
public static List<CurrentItem> Get(this DbSet<CurrentItem> item, int tierId, string contractId)
{
List<CurrentItem> items = ctx.Items.OfType<CurrentItem>().Where(x => x.TierId == tierId).ToList();
if (items == null)
{
GPOPricing.AS400Models.ItemCollection collection = new GPOPricing.AS400Models.ItemCollection().Get(contractId);
foreach (var c in collection)
{
CurrentItem target = new CurrentItem();
target.Price = c.DirectPriceEaches;
target.SKU = c.LongItemNbr;
target.Description = c.Description;
target.ProductLine = c.ProductLine;
items.Add(target);
}
}
else
{
foreach (var i in items)
{
GPOPricing.AS400Models.Item as400Item = new GPOPricing.AS400Models.ItemCollection().GetBySKU(i.SKU);
i.Description = as400Item.Description;
i.ProductLine = as400Item.ProductLine;
}
}
return items;
}
}
The problem I'm having is accessing it - CurrentItem is a subtype of Item. So I've tried:
db.Items.Get (doesn't work)
and I have tried
db.Items.OfType<CurrentItem>().Get (doesn't work)
Any suggestions?

I found that I had to use the base type and create a method for each subtype:
public static class CurrentItemExtensions
{
static GPOPricingEntities ctx = new GPOPricingEntities();
public static List<CurrentItem> GetCurrentItems(this DbSet<Item> item, int tierId, string contractId)
{
List<CurrentItem> items = ctx.Items.OfType<CurrentItem>().Where(x => x.TierId == tierId).ToList();
if (items.Count() == 0)
{
GPOPricing.AS400Models.ItemCollection collection = new GPOPricing.AS400Models.ItemCollection().Get(contractId);
foreach (var c in collection)
{
CurrentItem target = new CurrentItem();
target.Price = c.DirectPriceEaches;
target.SKU = c.LongItemNbr;
items.Add(target);
}
}
else
{
foreach (var i in items)
{
GPOPricing.AS400Models.Item as400Item = new GPOPricing.AS400Models.ItemCollection().GetBySKU(i.SKU);
}
}
return items;
}
}

Related

AutomationPeer.GetChildrenCore () only reports first child to VisualStudio.TestTools

I'm not able to override GetChildrenCore correctly. I use this for a Canvas to get information about it's children (Line, Rectangle).
The output correctly indicates the first child but misses the second. Even though the Canvas already contains both.
Custom Canvas
Custom Line Childs of Canvas parent: 2
Instead it should be like this:
Custom Canvas
Custom Line Childs of Canvas parent: 2
Custom Rectangle Childs of Canvas parent: 2
App side side:
public class ElementAP : FrameworkElementAutomationPeer
{
private FrameworkElement Owner = null;
private Int32 Count = 0;
public ElementAP(FrameworkElement owner, Int32 count) : base (owner)
{
Owner = owner;
Count = count;
}
protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.Custom;
}
protected override string GetClassNameCore()
{
return $"{Owner.GetType().Name} Childs of Canvas parent: {Count}";
}
}
public class CanvasAP : FrameworkElementAutomationPeer
{
public CanvasAP(Windows.UI.Xaml.Controls.Canvas owner) : base(owner)
{
}
protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.Custom;
}
protected override string GetClassNameCore()
{
return "Canvas";
}
protected override IList<AutomationPeer> GetChildrenCore()
{
var owner = (Windows.UI.Xaml.Controls.Canvas)Owner;
var list = new List<AutomationPeer> ();
foreach (var child in owner.Children)
{
var peer = new ElementAP(child as FrameworkElement, owner.Children.Count);
list.Add(peer);
}
return list;
}
}
UI Testing side:
private static string WalkTree(UITestControl element, Int32 level = 0)
{
var children = element.GetChildren();
var str = "";
foreach (var c in children)
{
str += GetElementString(c, level);
str += WalkTree(c, level + 1);
}
return str;
}
private static string GetElementString(UITestControl element, Int32 level = 0)
{
var xaml = element as XamlControl;
var str = "";
for (var i = 0; i < level; i++)
str += " ";
str += $"{element.ControlType} {element.ClassName} {element.Name} {xaml?.AutomationId ?? ""}\n";
return str;
}
I finally found an answer. When using a cache for the children`s AutomationPeers it works perfectly.
public class ElementAP : FrameworkElementAutomationPeer
{
public UIElement Element { get { return Owner; } }
public ElementAP(FrameworkElement owner) : base(owner)
{
}
protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.Custom;
}
protected override string GetClassNameCore()
{
return Owner.GetType().Name;
}
}
public class CanvasAP : FrameworkElementAutomationPeer
{
private List<ElementAP> _cachedAutomationPeers = new List<ElementAP>();
public CanvasAP(Windows.UI.Xaml.Controls.Canvas owner) : base(owner)
{
}
protected override AutomationControlType GetAutomationControlTypeCore()
{
return AutomationControlType.Custom;
}
protected override string GetClassNameCore()
{
return "Canvas";
}
protected override IList<AutomationPeer> GetChildrenCore()
{
var owner = (Windows.UI.Xaml.Controls.Canvas)Owner;
if (owner.Children.All(c => c is CanvasA))
return base.GetChildrenCore();
var list = new List<ElementAP>();
foreach (var child in owner.Children)
{
var peer = _cachedAutomationPeers.FirstOrDefault(p => p.Element == child) ?? new ElementAP(child as FrameworkElement);
list.Add(peer);
}
_cachedAutomationPeers = list;
return list.Cast<AutomationPeer>().ToList();
}
}

Linq: group by with a non-unique key (preserving order)

Suppose the list is like a-a-a-b-b-a-c-c-c-a-a, how do I get the groups
{a-a-a}-{b-b}-{a}-{c-c-c}-{a-a}?
So not what I want is: {a-a-a-a-a-a}-{b-b}-{c-c-c}.
Try following code;
var list = new List<string>
{
"a","a","a","b","b","a","c","c","c","a","a"
};
int index = 0;
string lastItem = "";
var groupedList =
list
.Select(x =>
{
if (lastItem != x)
{
index++;
lastItem = x;
}
return new { Item = x,Index = index };
})
.GroupBy(grp => grp)
.Select(grp => grp.Select(x => x.Item).ToList())
.ToList();
Output
a,a,a
b,b
a
c,c,c
a,a
public static List<(string key, List<T> items)> GroupByConsecutive<T>(
IEnumerable<T> items,
Func<T, string> key)
{
var list = new List<(string key, List<T> items)>();
foreach (var item in items)
{
if (list.Count == 0 || list.Last().key != key(item))
{
list.Add((key(item), new List<T> { item }));
}
else
{
list.Last().items.Add(item);
}
}
return list;
}

Initializing[Unavailable#]-failed to lazily initialize a collection, no session or session was closed

I have one class called fixture which has two properties description and date. Now i dont want to operate directly on thi stwo propert but i wanted to operate on Some collection object "Allattribute" in this example which i m feeling while in get and set of each property.
retrival is working fine but while persisting i m getting this error
Initializing[Unavailable#]-failed to lazily initialize a collection, no session or session was closed.
Code for Model and Mapper is as below:
public class Fixture : EntityBase
{
public Fixture() {
//PogObject = new List<PogObject>();
//ObjectFixtureDate = new List<Objectfixturedate>();
}
public virtual long IDPOGObject { get; set; }
private ObservableCollection<DictionaryEntry> m_allattributes = new ObservableCollection<DictionaryEntry>();
private IList<FixtureDate> mFixtureDate = new List<FixtureDate>();
private IList<FixtureDesc> mFixtureDesc = new List<FixtureDesc>();
private IList<FixtureFlag> mFixtureFlag = new List<FixtureFlag>();
public virtual IList<FixtureDate> FixtureDate
{
get
{
mFixtureDate.Clear();
foreach (var item in m_allattributes)
{
if (item.Value is FixtureDate)
{
mFixtureDate.Add((FixtureDate)item.Value);
}
}
return mFixtureDate;
}
set
{
//try
//{
if (value.Count != 0)
{
foreach (var item in value)
{
DictionaryEntry dEntry = new DictionaryEntry(item.Dictionary.DictionaryName, item);
if (!m_allattributes.Contains(dEntry))
{
m_allattributes.Add(dEntry);
}
}
}
//}
//catch (Exception ex)
//{
//}
mFixtureDate = value;
}
}
public virtual IList<FixtureDesc> FixtureDescription
{
get
{
mFixtureDesc.Clear();
foreach (var item in m_allattributes)
{
if (item.Value is FixtureDesc)
{
if (!mFixtureDesc.Contains((FixtureDesc)item.Value))
{
mFixtureDesc.Add((FixtureDesc)item.Value);
}
}
}
return mFixtureDesc;
}
set
{
if (value.Count != 0)
{
foreach (var item in value)
{
DictionaryEntry dEntry = new DictionaryEntry(item.Dictionary.DictionaryName, item);
// DictionaryEntry dEntry = new DictionaryEntry(item.DescNum, item);
if (!m_allattributes.Contains(dEntry))
{
m_allattributes.Add(dEntry);
}
}
}
mFixtureDesc = value;
}
}
}
Mapper Class:
public class FixtureMap : ClassMap {
public FixtureMap() {
Table("SPOG_ObjectFixture");
Not.LazyLoad();
Id(x => x.IDPOGObject).GeneratedBy.Foreign("PogObject");
HasMany(x => x.FixtureDescription).Cascade.AllDeleteOrphan().Inverse().Not.LazyLoad().KeyColumn("IDPOGObject").Fetch.Join();
HasMany(x => x.FixtureDate).Cascade.AllDeleteOrphan().Inverse().Not.LazyLoad().KeyColumn("IDPOGObject");
}
}

How do I apply a default IComparable<T> in a Linq OrderBy clause

I have a type which has a default sort order as it implements IComparable<T> and IComparable. I'm not getting the results I expect from LINQ , basically it looks as if the IComparable<T> which the type implements is not being applied.
I thought I would get the result I want with an expression in the form:
var result = MyEnumerable<T>.OrderBy(r => r);
where T itself implements IComparable<T>. It's not happening.
I can see related questions where specific IComparable<T> classes are specified for the sort, but I can't find one which uses the default IComparable<T> implemented by T itself.
My syntax is clearly incorrect. What is the correct syntax please?
Thanks in advance.
OrderBy uses the default comparer Comparer<T>.Default which in turn will default to use the IComparable<T> implementation for T, or the non-generic IComparable if the former does not exist.
This code works:
public class Program
{
static void Main(string[] args)
{
var list = new List<Stuff>
{
new Stuff("one"),
new Stuff("two"),
new Stuff("three"),
new Stuff("four")
};
var sorted = list.OrderBy(x => x);
foreach (var stuff in sorted)
{
Console.Out.WriteLine(stuff.Name);
}
}
}
public class Stuff : IComparable<Stuff>
{
public string Name { get; set; }
public Stuff(string name)
{
Name = name;
}
public int CompareTo(Stuff other)
{
return String.CompareOrdinal(Name, other.Name);
}
}
public static class GenericSorter
{
public static IOrderedEnumerable<T> Sort<T>(IEnumerable<T> toSort, Dictionary<string, SortingOrder> sortOptions)
{
IOrderedEnumerable<T> orderedList = null;
foreach (KeyValuePair<string, SortingOrder> entry in sortOptions)
{
if (orderedList != null)
{
if (entry.Value == SortingOrder.Ascending)
{
orderedList = orderedList.ApplyOrder<T>(entry.Key, "ThenBy");
}
else
{
orderedList = orderedList.ApplyOrder<T>(entry.Key, "ThenByDescending");
}
}
else
{
if (entry.Value == SortingOrder.Ascending)
{
orderedList = toSort.ApplyOrder<T>(entry.Key, "OrderBy");
}
else
{
orderedList = toSort.ApplyOrder<T>(entry.Key, "OrderByDescending");
}
}
}
return orderedList;
}
private static IOrderedEnumerable<T> ApplyOrder<T>(this IEnumerable<T> source, string property, string methodName)
{
ParameterExpression param = Expression.Parameter(typeof(T), "x");
Expression expr = param;
foreach (string prop in property.Split('.'))
{
expr = Expression.PropertyOrField(expr, prop);
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), expr.Type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, param);
MethodInfo mi = typeof(Enumerable).GetMethods().Single(
method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(T), expr.Type);
return (IOrderedEnumerable<T>)mi.Invoke(null, new object[] { source, lambda.Compile() });
}
}

EmitMapper and List

It's the first time that I use EmitMapper.
I have a list of object ex: Customer and I would like to map this list in a ienumerable of CustomerDTO how can I do that?
Tnx
It's straightforward if you have a list and want to convert it to list of DTOs:
var mapper = ObjectMapperManager.DefaultInstance.GetMapper<Customer, CustomerDTO>();
IEnumerable<CustomerDTO> dtos = listOfCustomer.Select(mapper.map);
The preblem is when the list is in another object, for example User and UserDTO:
class User {
public List<Customer> Customers { get; set; }
}
class UserDTO {
public IEnumerable<CustomerDTO> Customers { get; set; }
}
It seems that EmitMapper does not support conversion from List to Enumerable. A way to support it would be:
var customerMapper = ObjectMapperManager
.DefaultInstance.GetMapper<Customer, CustomerDTO>();
var mapper = ObjectMapperManager.DefaultInstance
.GetMapper<User, UserDTO>(
new DefaultMapConfig()
.ConvertUsing<List<Customer>, IEnumerable<CustomerDTO>>(
a => a.Select(customerMapper.Map))
);
This can be done creating a custom class, implementing the interface "ICustomConverterProvider" and adding a ConvertGeneric to the "DefaultMapConfig".
Looking on the source code of EmitMapper, i found a class named "ArraysConverterProvider", which is the default generic converter from ICollections to Arrays.
Adapting the code from this class to work with IEnumerable collections:
class GenericIEnumerableConverterProvider : ICustomConverterProvider
{
public CustomConverterDescriptor GetCustomConverterDescr(
Type from,
Type to,
MapConfigBaseImpl mappingConfig)
{
var tFromTypeArgs = DefaultCustomConverterProvider.GetGenericArguments(from);
var tToTypeArgs = DefaultCustomConverterProvider.GetGenericArguments(to);
if (tFromTypeArgs == null || tToTypeArgs == null || tFromTypeArgs.Length != 1 || tToTypeArgs.Length != 1)
{
return null;
}
var tFrom = tFromTypeArgs[0];
var tTo = tToTypeArgs[0];
if (tFrom == tTo && (tFrom.IsValueType || mappingConfig.GetRootMappingOperation(tFrom, tTo).ShallowCopy))
{
return new CustomConverterDescriptor
{
ConversionMethodName = "Convert",
ConverterImplementation = typeof(GenericIEnumerableConverter_OneTypes<>),
ConverterClassTypeArguments = new[] { tFrom }
};
}
return new CustomConverterDescriptor
{
ConversionMethodName = "Convert",
ConverterImplementation = typeof(GenericIEnumerableConverter_DifferentTypes<,>),
ConverterClassTypeArguments = new[] { tFrom, tTo }
};
}
}
class GenericIEnumerableConverter_DifferentTypes<TFrom, TTo> : ICustomConverter
{
private Func<TFrom, TTo> _converter;
public IEnumerable<TTo> Convert(IEnumerable<TFrom> from, object state)
{
if (from == null)
{
return null;
}
TTo[] result = new TTo[from.Count()];
int idx = 0;
foreach (var f in from)
{
result[idx++] = _converter(f);
}
return result;
}
public void Initialize(Type from, Type to, MapConfigBaseImpl mappingConfig)
{
var staticConverters = mappingConfig.GetStaticConvertersManager() ?? StaticConvertersManager.DefaultInstance;
var staticConverterMethod = staticConverters.GetStaticConverter(typeof(TFrom), typeof(TTo));
if (staticConverterMethod != null)
{
_converter = (Func<TFrom, TTo>)Delegate.CreateDelegate(
typeof(Func<TFrom, TTo>),
null,
staticConverterMethod
);
}
else
{
_subMapper = ObjectMapperManager.DefaultInstance.GetMapperImpl(typeof(TFrom), typeof(TTo), mappingConfig);
_converter = ConverterBySubmapper;
}
}
ObjectsMapperBaseImpl _subMapper;
private TTo ConverterBySubmapper(TFrom from)
{
return (TTo)_subMapper.Map(from);
}
}
class GenericIEnumerableConverter_OneTypes<T>
{
public IEnumerable<T> Convert(IEnumerable<T> from, object state)
{
if (from == null)
{
return null;
}
return from;
}
}
This code is just a copy with a minimum of adaptation as possible and can be applyed to objects with many levels of hierarchy.
You can use the above code with the following command:
new DefaultMapConfig().ConvertGeneric(
typeof(IEnumerable<>),
typeof(IEnumerable<>),
new GenericIEnumerableConverterProvider());
This saved my day and I hope to save yours too! hehehe

Resources