ICollection properties Sorting in ASP.Core using LINQ - linq

How to sort Icollection properties.For example:
public class INSTRUCTOR
{
public int Id
public string Name
public PersonalDetails PersonalDetail
}
public class PersonalDetails
{
public int Id;
public string Firstname;
public ICollection<Emails> Emails;
}
public class Emails
{
public int Id;
public string Email;
}
Now I have the list pages of instructor and I got all instructors like this:
var instructors = db.instructors.include(p=>p.personaldetails).Tolist();
I can sort using instructorName using the following Code:
instructors = instructors.OrderBy(i => i.PersonalDetail.Firstname).ToList();
But, I want to know how to do with using emails which is ICollection?

In some way you're going to have to flatten the Email collection in order to sort the parent collection by it, either by applying some aggregate function or by for example joining it into a string.
If you for example make the assumption that the first Email address in the list is the primary address, then it could make sense to sort only by this item;
var orderedList =
instructors.OrderBy(i => i.PersonalDetails.Emails.FirstOrDefault()?.EmailAddress).ToList();
You could also choose to order by the "smallest" email address in the list by applying the Min() aggregate function;
var orderedList =
instructors.OrderBy(x => x.PersonalDetails.Emails.Min(e => e.EmailAddress)).ToList();

Related

Spring JPA CriteriaQuery groupBy based on only one key in a Composite Primary Key (#EmbeddedId)

I am trying to write a criteriaQuery and group results based on emailId, which is one of the keys of a composite PK, embedded into one of my Entity classes.
The method that returns the specification is as follows :
public static Specification < User > getSpecification(Integer id) {
return (root, query, criteriaBuilder) - > {
var predicates = new ArrayList<Predicate()>;
predicates.add(criteriaBuilder.equal(root.get("indexId"), id));
query.groupBy(root.get("details").get("emailId"));
return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
};
}
The entity class:
public class User {
#EmbeddedId private Details details;
private String name;
private String status;
}
#Embeddable
public class Details {
private String emailId;
private Integer branchId;
}
I have skipped some annotations.
I want to group the results in a way where emailId remains unique, even if branchId changes. In essence for data where there are 3 rows of same emailId but different branchId, I should only fetch 1 result.
It seems it throws an error and asks for both components of the composite key to be passed in the query.groupBy statement.
If you could please help me figure out the issue.

Automapper to Map two List Classes of different structure and also Memberwise explicit Mapping

I have 4 classes namely ClassA, ClassADto, ClassAA(inner class to ClassA) and the final Result class.
ClassAA
{
public int HouseNumber{get;set;}
public string StreetName{get;set;}
public string State{get;set;}
}
ClassA
{
public int Age{get;set;}
public string Name{get;set;}
public ClassAA AObj[get;set;}
}
ClassADto
{
public int Age{get;set;}
public string Name{get;set;}
}
class Result
{
public string StreetName{get;set;}
public int TotalCount{get;set;}
public int TodaysDate{get;set;}
public List<ClassADto> AObjectsList{get;set;}
}
Now my aim is map the 'Result' class with the List of ClassA object to fill it the property 'AObjectsList' as below:
Result data= map mapper.map>(obj);
Also at the same time in automapper i want to use custom function either using 'Resolve' or 'AfterMap' to set properties like 'TodaysDate' to current datetime of system and property 'TotalCount' by counting the number of data.
I tried in many ways using 'CreateMap' and also used 'ForMembers' as from 'classAA' we only need the 'StreetName' but it didn't work. Need some help please.
One time typing approach ;)
public static Result ToResult(this List<ClassA> users)
{
return new Result
{
TotalCount = users.Count,
TodaysDate = DateTime.Today,
AObjectsList = users
.Select(user => new ClassADto
{
Name = user.Name,
Age = user.Age
})
.ToList()
};
}
// Usage
var users = new List<ClassA> { new ClassA(), new ClassA() };
var result = users.ToResult();

Projection create new field

I have a class that represents a user date of birth in two separated fields
public class User {
private int yearOfBirth;
private int monthOfBirth;
}
Is it possible to make a projection that exports the user age? I know we can concatenate fields using #Value.
The easiest way to resolve the problem (if you can add code to the domain class) is to add a method in the user class like the one below:
#JsonIgnore
public int getAge() {
return Period.between(
LocalDate.of(dobYear, dobMonth, 1),
LocalDate.now()
).getYears();
}
You can add the #JsonIgnore to block spring from exporting an "age" field when your entity is serialized. After adding that method you can create projection like the one below:
#Projection(name = "userAge ", types = {User.class})
public interface UserAge {
#Value("#{target.getAge()}")
Integer getAge();
}
Something like this, for example:
public class UserAgeDto {
private int yearOfBirth;
private int monthOfBirth;
public UserAgeDto(int yearOfBirth, int monthOfBirth) {
// constructor implementation...
}
public int getAge() {
// age calculation...
}
}
public interface UserRepo extends JpaRepository<User, Long> {
#Query("select new com.example.myapp.dto.UserAgeDto(u.yearOfBirth, u.monthOfBirth) from User u where u = ?")
UserAgeDto getUserAgeDto(User user);
}
Some info

AutoMapper mapping model list

I am trying to use AutoMapper for the first time and have some problems with it.
My code is below and I get error below. Maybe someone could show how to map the list of models?
cannot convert from 'System.Linq.IQueryable<AnonymousType#1>' to 'Entity.Product' C:\Users\Administrator\Projects\PC\trunk\PC\Controllers\AdminController.cs 37 100 PC
public class ProductViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public int UsersCount { get; set; }
}
var products = _repository.GetProducts(true).Select(p=> new
{
p.Id,
p.Name,
UsersCount = 0
});
Mapper.CreateMap<Product, ProductViewModel>();
ViewData["Products"] = Mapper.Map<IEnumerable<Product>, IEnumerable<ProductViewModel>>(products); //Error appears on products object
//Product domain model(linq2sql generated model)
public partial class Product : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _Id;
private bool _Active;
private System.Nullable<int> _Sort;
private System.Nullable<int> _Category;
private string _Name;
private int _ProductTypeId;
private decimal _Price;
private System.Nullable<int> _Months;
private System.Nullable<int> _Credits;
private string _Features;
private string _BlockReason;
private string _BuyUrl1;
private string _BuyUrl2;
private bool _UsersManager;
}
In your LINQ query you select an anonymous object. Make sure you select a Product which is your source type (or more specifically IEnumerable<Product>):
IEnumerable<Product> products = _repository.GetProducts(true);
IEnumerable<ProductViewModel> productsViewModel = Mapper.Map<IEnumerable<Product>, IEnumerable<ProductViewModel>>(products);
return View(productsViewModel);
Also do not call Mapper.CreateMap<TSource, TDest> inside your controller action. This must be called only once in the lifetime of the AppDomain, ideally in your Application_Start.
Also notice that I have gotten rid of ViewData which is a great thing. You don't need ViewData. You are working with view models. That's what they are supposed to do. Contain information that will be needed by your view in a strongly typed manner.

Create a list of one object type from a list of another using Linq

If I have classes of Type A and B:
public class A
{
public int TotalCount;
public string Title;
}
public class B
{
public int Count;
public string Title;
}
I have a list of instances A instances, what is the most efficient way to create and populate a List of type B using Linq?
List<B> listB = listA.Select(a => new B()
{
Count = a.TotalCount,
Title = a.Title
}).ToList();
Does the same as eduncan's solution with different syntax. Take your pick..
var list =
from a in TableA
select new B {
Count = a.TotalCount,
Title = a.Title
};
You new up an instance of B in your select clause, and assign the properties using the inline property assignment feature in C# 3.0.
The advantages of mapping it inline comes from deferred execution of your Linq statement. Linq will map the modified statement, and execute it from your IQueryable. For example:
public class ClassA
{
public int TotalCount;
public string Title;
}
public class ClassB
{
public int Count;
public string Title;
}
public IQueryable<ClassB> FetchAllOfClassB()
{
var list =
from a in TableOfClassA
select new ClassB {
Count = a.TotalCount,
Title = a.Title
};
return list.AsQueryable();
}
Technically, the AsQueryable() is a bit redundant. Sometimes I use it to make it a point, others say it is absolutely required. None the less, the list object itself is IQueryable of ClassB.
Then you can call FetchAllOfClassB() further up the chain, and use IQuerable. It's pretty slick, and efficient.
Hmm, off the top of my head (so there will probably be errors!):
List< B > = from a in listOfA select new B(a.count, a.title);
might do the trick.

Resources