I have a list of objects, GroupStudentStatus, that I need to make distinct.
I wrote the class below to do this.
The 2 attributes that are relevant are GroupStudentStatus.IsLastActionRemoved (DateTime) and GroupStudentStatus.Student.Guid.
protected List<GroupStudentStatus> RemovedStudents
{
get
{
return AllStudents.Where(s => s.IsLastActionRemoved).Distinct().OrderByDescending(d => d.LastActionDate).ToList();
}
}
public class GroupStudentStatusComparer : IEqualityComparer<GroupStudentStatus>
{
public GroupStudentStatus Compare(GroupStudentStatus x, GroupStudentStatus y)
{
//get the student that was last removed
if (!Equals(x, y))
{
return x.LastActionDate > y.LastActionDate ? x : y;
}
return x;
}
public bool Equals(GroupStudentStatus x, GroupStudentStatus y)
{
return x.Student.Guid.Equals(y.Student.Guid);
}
public int GetHashCode(GroupStudentStatus obj)
{
return obj.Student.Guid.GetHashCode();
}
}
I think this is right, except I can't figure out how to test it.
I was trying to do this:
return AllStudents.Where(s => s.IsLastActionRemoved)
.Distinct(new GroupStudentStatusComparer((x, y) => x.Compare(x,y)))
.OrderByDescending(d => d.LastActionDate).ToList();
return AllStudents.Where(s => s.IsLastActionRemoved)
.Distinct(new GroupStudentStatusComparer())
.OrderByDescending(d => d.LastActionDate)
.ToList();
return AllStudents.Where(s => s.IsLastActionRemoved).GroupBy(gss => gss.Student).Select(g => new GroupStudentStatus(g.Key, g.Select(gss2 => gss2.LastActionDate).Max(), true)).OrderByDescending(d => d.LastActionDate).ToList();
Ended up using groupBy.
Related
I have this code generating a "Calculated" or "Output" property every time another property changes. The derived property is generated correctly, but since CreateBlurImage is a bit long-running, the UI gets frozen as it runs.
What would be the correct way to get the same end result, but asynchronously?
EDIT this is the current version of my code after some suggestions from #Shane, but still with an UI that keeps frozen until all the processing completes. Notice that there are quite a handful of cascading updates, if that matters:
public ColetaIsis Model { get; private set; }
public string NomePaciente { get { return Model?.NomePaciente; } }
public DateTime DataRealização { get { return Model.DataRealização; } }
public BitmapSource Listras
{
get { return _listras; }
set { this.RaiseAndSetIfChanged(ref _listras, value); }
}
BitmapSource _listras;
public double[,] Grayscale { get { return _grayscale.Value; } }
readonly ObservableAsPropertyHelper<double[,]> _grayscale;
public double[,] BlurMenor { get { return _blurMenor.Value; } }
readonly ObservableAsPropertyHelper<double[,]> _blurMenor;
public double[,] BlurMaior { get { return _blurMaior.Value; } }
readonly ObservableAsPropertyHelper<double[,]> _blurMaior;
public double[,] Diferença { get { return _diferença.Value; } }
readonly ObservableAsPropertyHelper<double[,]> _diferença;
public BitmapSource FiltradaMenor { get { return _filtradaMenor?.Value; } }
readonly ObservableAsPropertyHelper<BitmapSource> _filtradaMenor;
public BitmapSource FiltradaMaior { get { return _filtradaMaior?.Value; } }
readonly ObservableAsPropertyHelper<BitmapSource> _filtradaMaior;
public BitmapSource ImagemDiferença { get { return _imagemDiferença?.Value; } }
readonly ObservableAsPropertyHelper<BitmapSource> _imagemDiferença;
public IEnumerable<ScatterPoint> Picos => _picos;
IEnumerable<ScatterPoint> _picos;
// CONSTRUTOR
public ColetaIsisViewModel(ColetaIsis model)
{
this.WhenAnyValue(x => x.Listras)
.Where(item => item != null)
.ObserveOn(RxApp.TaskpoolScheduler)
.Select(im => GetArray.FromChannels(im, 0, 1))
//.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.Grayscale, out _grayscale, scheduler:RxApp.MainThreadScheduler);
this.WhenAnyValue(x => x.Grayscale)
.Where(item => item != null)
.ObserveOn(RxApp.TaskpoolScheduler)
.Select(ar => Gaussian.GaussianConvolution(ar, 1.5))
//.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.BlurMenor, out _blurMenor, scheduler: RxApp.MainThreadScheduler);
this.WhenAnyValue(x => x.BlurMenor)
.Where(item => item != null)
.ObserveOn(RxApp.TaskpoolScheduler)
.Select(ar => Gaussian.VerticalGaussianConvolution(ar, 5))
//.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.BlurMaior, out _blurMaior, scheduler: RxApp.MainThreadScheduler);
this.WhenAnyValue(x => x.BlurMenor, x => x.BlurMaior)
.Where(tuple => tuple.Item1 != null && tuple.Item2 != null)
.ObserveOn(RxApp.TaskpoolScheduler)
.Select(tuple => ArrayOperations.Diferença(tuple.Item1, tuple.Item2))
//.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.Diferença, out _diferença, scheduler: RxApp.MainThreadScheduler);
this.WhenAnyValue(x => x.BlurMenor)
.Where(item => item != null)
.ObserveOn(RxApp.TaskpoolScheduler)
.Select(ar => { ConversorImagem.Converter(ar, out BitmapSource im); return im; })
//.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.FiltradaMenor, out _filtradaMenor, scheduler: RxApp.MainThreadScheduler);
this.WhenAnyValue(x => x.BlurMaior)
.Where(item => item != null)
.ObserveOn(RxApp.TaskpoolScheduler)
.Select(ar => { ConversorImagem.Converter(ar, out BitmapSource im); return im; })
//.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.FiltradaMaior, out _filtradaMaior, scheduler: RxApp.MainThreadScheduler);
this.WhenAnyValue(x => x.Diferença)
.Where(item => item != null)
.ObserveOn(RxApp.TaskpoolScheduler)
.Select(ar => { ConversorImagem.Converter(ar, out BitmapSource im); return im; })
//.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.ImagemDiferença, out _imagemDiferença, scheduler: RxApp.MainThreadScheduler);
Model = model;
Listras = Model.Listras; // fires up the initial cascading updates
}
Would any of these ways work? This way the CreateBlurImage part is done off the UIThread
public MyClass()
{
this.WhenAnyValue(x => x.StripedImage)
.ObserveOn(RxApp.TaskpoolScheduler)
.Select(im => CreateBlurImage(im))
.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.Filtered, out _filtered);
}
or I'm pretty sure specifying the scheduler on ToProperty will be the same as above
public MyClass()
{
this.WhenAnyValue(x => x.StripedImage)
.ObserveOn(RxApp.TaskpoolScheduler)
.Select(im => CreateBlurImage(im))
.ToProperty(this, x => x.Filtered, out _filtered, scheduler:RxApp.MainThreadScheduler);
}
public static List<Reservations> getReservations(int reservationId)
{
return hoteldb.hotel_reservations.Where(x => x.reservation_id == reservationId).ToList();
}
I converted the values, I made sure that everything is good but to no avail.
It's not really working even the code below:
public List<Reservations> getReservations(int reservationId)
{
return hoteldb.hotel_reservations.Where(x => x.reservation_room_desc == "sdfsfs").ToList();
}
Maybe getReservations() should use the type : List<hotel_reservations> ?
public List<hotel_reservations> getReservations(int reservationId)
{
var y = hoteldb.hotel_reservations
.Where(x => x.reservation_room_desc == "sdfsfs")
.ToList();
return y;
}
I have a list of lists which looks like the following
public class FilteredVM
{
public int ID { get; set; }
public string Name { get; set; }
public string Number { get; set; }
}
List<List<FilteredVM>> groupedExpressionResults = new List<List<FilteredVM>>();
I would like to Intersect the lists within this list based upon the ID's, whats the best way to tackle this?
Here's an optimized extension method:
public static HashSet<T> IntersectAll<T>(this IEnumerable<IEnumerable<T>> series, IEqualityComparer<T> equalityComparer = null)
{
if (series == null)
throw new ArgumentNullException("series");
HashSet<T> set = null;
foreach (var values in series)
{
if (set == null)
set = new HashSet<T>(values, equalityComparer ?? EqualityComparer<T>.Default);
else
set.IntersectWith(values);
}
return set ?? new HashSet<T>();
}
Use this with the following comparer:
public class FilteredVMComparer : IEqualityComparer<FilteredVM>
{
public static readonly FilteredVMComparer Instance = new FilteredVMComparer();
private FilteredVMComparer()
{
}
public bool Equals(FilteredVM x, FilteredVM y)
{
return x.ID == y.ID;
}
public int GetHashCode(FilteredVM obj)
{
return obj.ID;
}
}
Like that:
series.IntersectAll(FilteredVMComparer.Instance)
You could just write
series.Aggregate((a, b) => a.Intersect(b, FilteredVMComparer.Instance))
but it 'd be wasteful because it'd have to construct multiple sets.
Intersect will work when the type are dead equals, which in your case won't apply because you haven't implemented the GetHashCode and Equals methods, which is the best and complete way.
Thus, If you only intended to take elements that contains in both lists, than the following solution will suit you right.
Assuming list1 and list2 are type List<FilteredVM> than, The most simple way, will be doing this:
var intersectByIDs = list1.Where(elem => list2.Any(elem2 => elem2.ID == elem.ID));
If you are a fan of one-liner solutions you can use this:
List<FilteredVM> result = groupedExpressionResults.Aggregate((x, y) => x.Where(xi => y.Select(yi => yi.ID).Contains(xi.ID)).ToList());
And if you just want the IDs you can just add .Select(x => x.ID), like this:
var ids = groupedExpressionResults.Aggregate((x, y) => x.Where(xi => y.Select(yi => yi.ID).Contains(xi.ID)).ToList()).Select(x => x.ID);
Working Demo
I'm looking for a way to get total price count from the Costs list in my object. I can't get Projections.Sum to work in my QueryOver so I tried another way but I'm having problems with it. I want to use a unmapped property in my QueryOver. I found this example but it's giving an error.
Object:
public class Participant
{
public int Id { get; set; }
public double TotalPersonalCosts { get { return Costs.Where(x => x.Code.Equals("Persoonlijk") && x.CostApprovalStatus == CostApprovalStatus.AdministratorApproved).Sum(x => x.Price.Amount); } }
public IList<Cost> Costs { get; set; }
}
The property TotalPersonalCosts is not mapped and contains the total price count.
Extension Class:
public static class ParticipantExtensions
{
private static string BuildPropertyName(string alias, string property)
{
if (!string.IsNullOrEmpty(alias))
{
return string.Format("{0}.{1}", alias, property);
}
return property;
}
public static IProjection ProcessTotalPersonalCosts(System.Linq.Expressions.Expression expr)
{
Expression<Func<Participant, double>> w = r => r.TotalPersonalCosts;
string aliasName = ExpressionProcessor.FindMemberExpression(expr);
string totalPersonalCostName = ExpressionProcessor.FindMemberExpression(w.Body);
PropertyProjection totalPersonalCostProjection =
Projections.Property(BuildPropertyName(aliasName, totalPersonalCostName));
return totalPersonalCostProjection;
}
}
My QueryOver:
public override PagedList<AccountantViewInfo> Execute()
{
ExpressionProcessor.RegisterCustomProjection(
() => default(Participant).TotalPersonalCosts,
expr => ParticipantExtensions.ProcessTotalPersonalCosts(expr.Expression));
AccountantViewInfo infoLine = null;
Trip tr = null;
Participant pa = null;
Cost c = null;
Price p = null;
var infoLines = Session.QueryOver(() => tr)
.JoinAlias(() => tr.Participants, () => pa);
if (_status == 0)
infoLines.Where(() => pa.TotalCostApprovalStatus == TotalCostApprovalStatus.CostPrinted || pa.TotalCostApprovalStatus == TotalCostApprovalStatus.CostPaid);
else if (_status == 1)
infoLines.Where(() => pa.TotalCostApprovalStatus == TotalCostApprovalStatus.CostPrinted);
else
infoLines.Where(() => pa.TotalCostApprovalStatus == TotalCostApprovalStatus.CostPaid);
infoLines.WhereRestrictionOn(() => pa.Employee.Id).IsIn(_employeeIds)
.Select(
Projections.Property("pa.Id").WithAlias(() => infoLine.Id),
Projections.Property("pa.Employee").WithAlias(() => infoLine.Employee),
Projections.Property("pa.ProjectCode").WithAlias(() => infoLine.ProjectCode),
Projections.Property("tr.Id").WithAlias(() => infoLine.TripId),
Projections.Property("tr.Destination").WithAlias(() => infoLine.Destination),
Projections.Property("tr.Period").WithAlias(() => infoLine.Period),
Projections.Property("pa.TotalPersonalCosts").WithAlias(() => infoLine.Period)
);
infoLines.TransformUsing(Transformers.AliasToBean<AccountantViewInfo>());
var count = infoLines.List<AccountantViewInfo>().Count();
var items = infoLines.List<AccountantViewInfo>().ToList().Skip((_myPage - 1) * _itemsPerPage).Take(_itemsPerPage).Distinct();
return new PagedList<AccountantViewInfo>
{
Items = items.ToList(),
Page = _myPage,
ResultsPerPage = _itemsPerPage,
TotalResults = count,
};
}
Here the .Expression property is not found from expr.
I don't know what I'm doing wrong. Any help or alternatives would be much appreciated!
Solution with Projection.Sum() thx to xanatos
.Select(
Projections.Group(() => pa.Id).WithAlias(() => infoLine.Id),
Projections.Group(() => pa.Employee).WithAlias(() => infoLine.Employee),
Projections.Group(() => pa.ProjectCode).WithAlias(() => infoLine.ProjectCode),
Projections.Group(() => tr.Id).WithAlias(() => infoLine.TripId),
Projections.Group(() => tr.Destination).WithAlias(() => infoLine.Destination),
Projections.Group(() => tr.Period).WithAlias(() => infoLine.Period),
Projections.Sum(() => c.Price.Amount).WithAlias(() => infoLine.TotalPersonalCost)
);
You can't use unmapped columns as projection columns of a NHibernate query.
And the way you are trying to do it is conceptually wrong: the ParticipantExtensions methods will be called BEFORE executing the query to the server, and their purpose is to modify the SQL query that will be executed. An IProjection (the "thing" that is returned by ProcessTotalPersonaCosts) is a something that will be put between the SELECT and the FROM in the query. The TotalCosts can't be returned by the SQL server because the SQL doesn't know about TotalCosts
Says I have ListA={null,3,2,null}.
ListA.OrderBy(x=>x.ID) //would return me null,null,2,3
If my objective is to get 2,3,null,null, currently I can only think of extracting out the null item, and manually pump into the back.
Is there a clean approach where it will return me 2,3,null,null?
You can use OrderByDescending + ThenBy(assuming that it's aList<int?>):
var orderedList = ListA
.OrderByDescending(x => x.HasValue)
.ThenBy(x => x);
x.HasValue returns true or false where true is higher than false. That's why i'm using OrderByDescending.
If you want to sort the original list i would use List.Sort with a custom Compaison<T> that treats null as highest value:
ListA.Sort((a1, a2) => (a1 ?? int.MaxValue).CompareTo(a2 ?? int.MaxValue));
This is more efficient since it doesn't need to create a new list.
As an alternative to Tim's answer you could write your own IComparer<T> which does the custom sorting algorithm for you.
var array = list.OrderBy(x => x, new NullableIntComparer())
.ToArray();
class NullableIntComparer : IComparer<int?>
{
public int Compare(int? x, int? y)
{
if (x.HasValue && y.HasValue)
{
return x.Value.CompareTo(y.Value);
}
if (x.HasValue)
{
return -1;
}
if (y.HasValue)
{
return 1;
}
return 0;
}
}
Tried the following:
class Program
{
class A
{
public A(){}
public int? ID { get; set; }
}
static void Main(string[] args)
{
var listA = new List<A>
{
new A(){ID = null},
new A(){ID = 2},
new A(){ID = null},
new A(){ID = 3},
};
var result = listA.OrderByDescending(x => x.ID != null).ThenBy(x => x.ID);
foreach (var a in result)
{
Console.WriteLine(a.ID);
}
}
}