I have an aggregate root such as:
public class Parent
{
public Guid ParentId {get;set;}
public List<Children> {get;set;}
}
public class Children
{
public Guid ChildrenId {get;set;}
public Parent Parent {get;set;}
public string Name {get;set;}
}
I would like to add aditional property for sorting in Children class. It should help me sort Children by the creatinon order.
How can I implement that? The result in database should be like this:
ChildrenId | Name | Parent |ChildNumber
------------------------------------
guid1 | Name1 | 1 | 1
guid2 | Name2 | 1 | 2
guid3 | Name3 | 1 | 3
guid4 | Name1 | 2 | 1
guid5 | Name2 | 2 | 2
guid6 | Name3 | 2 | 3
I have some flat list that I want to sort in a pseudo-hierarchical order. Without materialization using .ToList(), only IQueryable.
I do the following:
public class DictionaryElement
{
[Key]
public Guid ID { get; set; }
[ForeignKey("ParentDictionaryElement")]
public Guid? ParentDictionaryElementID { get; set; }
public DictionaryElement ParentDictionaryElement { get; set; }
public ICollection<DictionaryElement> DictionaryElements { get; set; }
public String Value { get; set; }
}
public IQueryable<DictionaryElement> Foo(DbContext db)
{
IQueryable<DictionaryElement> allElementsQuery = db.DictionaryElements;
//... allElementsQuery.OrderBy(); ?
return allElementsQuery;
}
and data:
+----+---------------------------+
| ID | ParentDictionaryElementID |
+----+---------------------------+
| 1 | null |
| 2 | null |
| 3 | 1 |
| 4 | null |
| 6 | 3 |
| 5 | 2 |
+----+---------------------------+
I want to get IQueryable which will receive such data:
+----+---------------------------+
| ID | ParentDictionaryElementID |
+----+---------------------------+
| 1 | null |
| 3 | 1 |
| 6 | 3 |
| 2 | null |
| 5 | 2 |
| 4 | null |
+----+---------------------------+
The difference is that the second table is a hierarchy but in a flat, flat view, as it would have looked at normal display.
That is, the children immediately follow the parent
ID 1
ID 3 (parentID 1)
ID 6 (parentID 3)
ID 2
ID 5 (parentID 2)
ID 4
try this?
return allElementsQuery.OrderBy(m => m).AsQueryable();
I have two tables.
Table A, store the packages data.
table a
PackageId | From | To | Created
-----------------------------------------
a1 | Japan | USA | 2019/2/22 16:00
a2 | Japan | USA | 2019/2/23 16:00
a3 | Japan | USA | 2019/2/24 16:00
a4 | Japan | USA | 2019/2/25 16:00
a5 | Japan | USA | 2019/2/26 16:00
a6 | Japan | USA | 2019/2/27 16:00
a7 | Japan | USA | 2019/2/28 16:00
a8 | Japan | USA | 2019/3/1 16:00
a9 | Japan | USA | 2019/3/2 16:00
Table B, stores tracking history for each package. Multiple tracking records are generated for each package in transit.
table b
PackageId | StaffId | Action | UpdateDate
------------------------------------------------
a1 | s1 | Pick up | 2019/2/23 15:00
a1 | s1 | transmit | 2019/2/24 15:00
a1 | s2 | transmit | 2019/2/25 15:00
a1 | s1 | transmit | 2019/2/26 15:00
a1 | s1 | transmit | 2019/2/27 15:00
a1 | s2 | transmit | 2019/2/28 15:00
a1 | s1 | transmit | 2019/3/1 15:00
a1 | s1 | transmit | 2019/3/2 15:00
a1 | s2 | Deliver | 2019/3/3 15:00
I'm going to create a page with a list of packages and the latest status for each package. Like follow:
query result
Packages status page
PackageId | From | To | Last Action | UpdateDate
-------------------------------------------------------
a1 | Japan | USA | Deliver | 2019/3/3 15:00
a2 | Japan | USA | transmit | 2019/3/4 15:00
a3 | Japan | USA | transmit | 2019/3/5 15:00
a4 | Japan | USA | transmit | 2019/3/6 15:00
a5 | Japan | USA | transmit | 2019/3/7 15:00
a6 | Japan | USA | transmit | 2019/3/8 15:00
a7 | Japan | USA | transmit | 2019/3/9 15:00
a8 | Japan | USA | transmit | 2019/3/10 15:00
a9 | Japan | USA | transmit | 2019/3/11 15:00
I already have a SQL query string:
SELECT A.*, B.*
FROM [Table A] AS A
JOIN
(
SELECT TOP 1 WITH TIES *
FROM [Table B]
ORDER BY ROW_NUMBER() OVER (PARTITION BY PackageId ORDER BY UpdateDate DESC)
) AS B ON (b.PackageId = A.PackageId)
How I can convert above Sql string to Linq?
Or
How I can do query by EF / .Net core?
Given this class to store the result:
public class Result
{
public string PackageId { get; set; }
public string From { get; set; }
public string To { get; set; }
public DateTime UpdateDate { get; set; }
public string LastAction { get; set; }
}
And given aEntries containing As and bEntries containing Bs and
Using .Select():
List<Result> results = aEntries.AsEnumerable().Select(a =>
{
var lastB = bEntries.OrderByDescending(b => b.UpdateDate)
.First(b => b.PackageId == a.PackageId);
return new Result
{
PackageId = a.PackageId,
From = a.From,
To = a.To,
LastAction = lastB.Action,
UpdateDate = lastB.UpdateDate
};
}).ToList();
Alternatively, using .Join():
List<Result> = aEntries.Join(bEntries,
a => a.PackageId,
b => b.PackageId,
(a, b) => new Result()
{
PackageId = a.PackageId,
To = a.To,
From = a.From,
LastAction = b.Action,
UpdateDate = b.UpdateDate
})
.GroupBy(r => r.PackageId)
.Select(g => g.OrderByDescending(r => r.UpdateDate).First()).ToList();
All you should need is this:
Packages.Select(p =>
new {
Package = p,
LastAction = p.Actions.OrderByDescending(a => a.UpdateDate).FirstOrDefault()
})
Assuming your EF classes are setup this way:
public class Package
{
public int PackageId { get; set; }
public string From { get; set; }
public string To { get; set; }
public DateTime Created { get; set; }
public virtual ICollection<Action> Actions { get; set; }
}
public class Action
{
public int PackageId { get; set; }
public int StaffId { get; set; }
public string Action { get; set; }
public DateTime UpdateDate { get; set; }
}
Using WPF datagrid with MVVM structure (no code behind). I was wondering if it was possible to take a model property that normally shows up in a single column and use that to dynamically generate new columns.
Example
public Class1
{
public int Property1 { get; set; }
public string Property2 { get; set; }
public double Property3 { get; set; }
}
Mock up standard datagrid
|Property1|Property2|Property3|
| 1 | A | 1.25 |
| 2 | B | 2 |
| 3 | B | 3 |
Mock up desired datagrid
|Property1| A | B |
| 1 | 1.25 | |
| 2 | | 2 |
| 3 | | 3 |
There is no limit on what value Property2 can be.
I have a SQL Server four tables 'mapping' table MappingACVP as:
Author_ID | CoAuthor_ID | Venue_ID | Paper_ID | Year
----------------------------------------------------
677 | 42700 | 64309 | 812229 | 2005
677 | 42700 | 64309 | 812486 | 2005
677 | 42700 | 64309 | 818273 | 2005
677 | 42700 | 65182 | 812229 | 2005
... | ... | ... | ... | ...
... | ... | ... | ... | ...
By using Entity Framework, I got auto-generated code for all four tables included in Entity Framework Model i.e. Authors.cs, CoAuthors_New.cs, Venues_New.cs and Papers_New.cs as well, whereas tables other than Authors all have Author_ID as foreign key as a relationship.
The auto-generated code for class Authors.cs is as:
public partial class Authors
{
public Authors()
{
this.CoAuthors_New = new HashSet<CoAuthors_New>();
this.Papers_New = new HashSet<Papers_New>();
this.Venues_New = new HashSet<Venues_New>();
}
public int id { get; set; }
public int Author_ID { get; set; }
public string Author_Name { get; set; }
public virtual ICollection<CoAuthors_New> CoAuthors_New { get; set; }
public virtual ICollection<Papers_New> Papers_New { get; set; }
public virtual ICollection<Venues_New> Venues_New { get; set; }
}
Now if I want to declare a list as:
List<Authors> _eAthors = new List<Authors>();
and want to fetch values for filling this list from database (using MappingACVP table), whereas there is no Year property in Authors.cs class. So,
How will I fill eAuthors list from database?