I have 2 entities: Post and Article and I need to select both of them at once and sort by common property. I'm searching for some "join" possibilities, but all I'm finding are examples ending with something like ...where a.id == b.a_id which is not relevant in my case.
That's my models
class BaseEntity {
public int Id;
public DateTime AddedDate;
}
class Post extends BaseEntity {
public string Title;
}
class Article extends BaseEntity {
public string Name;
}
For that data:
Articles:
Id | Name | AddedDate
1 | AAA | 15/05/2020 18:00:00
2 | BBB | 17/05/2020 18:00:00
3 | CCC | 19/05/2020 18:00:00
Posts:
Id | Title | AddedDate
1 | DDD | 16/05/2020 18:00:00
2 | EEE | 18/05/2020 18:00:00
3 | FFF | 20/05/2020 18:00:00
when sorting by date DESC and limit it to 4 i need to end up with something like that, or something similar:
Article Id | PostId | Name | Title | AddedDate
NULL | 3 | NULL | FFF | 20/05/2020 18:00:00
3 | NULL | CCC | NULL | 19/05/2020 18:00:00
NULL | 2 | NULL | EEE | 18/05/2020 18:00:00
2 | NULL | BBB | NULL | 17/05/2020 18:00:00
Could someone advice me somehow? I need to do this in EF Core.
I could construct whole list similarly to this and manipulate on this, but this is very far from being optimal:
var results = (new List<BaseEntity>()).Concat(_context.Posts.ToList());
results = results.Concat(_context.Articles.ToList()).ToList();
//order
//take(x)
To achieve the result you need you will have to project both Post and Article to a shared typed, so first I created a ResultDto - which is representation of the type that you want to get:
public class ResultDto
{
public int? ArticleId { get; set; }
public int? PostId { get; set; }
public string Name { get; set; }
public string Title { get; set; }
public DateTime AddedDate { get; set; }
}
Then you have to project Articles to ResultDto, as well as Post to ResultDto, concat the results and sort like this:
var result = _dbContext
.Articles.Select(a => new ResultDto()
{
ArticleId = a.Id,
AddedDate = a.AddedDate,
Name = a.Name,
PostId = null,
Title = null
}).Concat(_dbContext.Posts.Select(p => new ResultDto()
{
PostId = p.Id,
AddedDate = p.AddedDate,
Title = p.Title,
ArticleId = null,
Name = null
}))
.OrderByDescending(r => r.AddedDate)
.ToList();
Btw since this Select is invoked on an IQueryable, you will have to select all of the properties (even if they would be NULL by default).
This results in following SQL query:
SELECT [t].[ArticleId], [t].[AddedDate], [t].[Name], [t].[PostId], [t].[Title]
FROM (
SELECT [a].[Id] AS [ArticleId], [a].[AddedDate], [a].[Name], NULL AS [PostId], NULL AS [Title]
FROM [Articles] AS [a]
UNION ALL
SELECT NULL AS [ArticleId], [p].[AddedDate], NULL AS [Name], [p].[Id] AS [PostId], [p].[Title]
FROM [Posts] AS [p]
) AS [t]
ORDER BY [t].[AddedDate] DESC
Related
I have this data available from the database:
|----------------|--------------|----------------|----------------|----------------|----------------|
| entity_name | provider_id | provider_name | product_id | product_name | country_name |
|----------------|--------------|----------------|----------------|----------------|----------------|
| test | 123 | Provider1 | 1 | Product1 | Russia |
|----------------|--------------|----------------|----------------|----------------|----------------|
| test | 123 | Provider1 | 2 | Product2 | Spain |
|----------------|--------------|----------------|----------------|----------------|----------------|
| test | 123 | Provider1 | 3 | Product3 | France |
|----------------|--------------|----------------|----------------|----------------|----------------|
| test | 456 | Provider2 | 3 | Product3 | France |
|----------------|--------------|----------------|----------------|----------------|----------------|
| test | 123 | Provider1 | 4 | Product4 | France |
And I have to map it to this model in C#:
public class EntityModel
{
public string EntityName { get; set; }
public List<ProviderModel> Providers { get; set; }
}
public class ProviderModel
{
public int ProviderID { get; set; }
public string ProviderName { get; set; }
public List<ProductModel> Products { get; set; }
public ProviderModel() { }
}
public class ProductModel
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public string CountryName { get; set; }
public ProductModel() { }
}
So basically I have to group by providers, for every provider I have to show the products:
{
"entityName": "string",
"providers": [
{
"ProviderID": 0,
"ProviderName": "string",
"products": [
{
"productId": 0,
"productName": "string",
"countryName": "string"
}
]
}
]
}
The sp that returns the data is like this:
SELECT DISTINCT
e.entity_name
,pro.provider_id
,pro.provider_name
,p.product_id
,p.product_name
,pc.country_name
FROM provider_product_table ppt
INNER JOIN product p ON ppt.product_id = p.product_id
INNER JOIN product_parent pp on pp.product_parent_id=p.product_parent_id
INNER JOIN provider pro ON pro.provider_id = ppt.provider_id
INNER JOIN product_country pc on pc.product_id=p.product_id
INNER JOIN entity e on e.product_parent_id=pp.product_parent_id
WHERE p.product_parent_id = #product_parent_id
ORDER BY p.product_id ASC
I tried a lot of groupBy versions but I get stuck at mapping the second list, the products one.
How can I achieve this? Thank you !
let's start with saying that your database is not well-structured or not created for your need, it will be more than good if you try to seprate your data into three table (entity,provider,product) with relationship between entity and provider , and relationship between your provider and the product table.
however, maybe you are developing new feature that it was not thinking at first to make your code or even your database more extensible.
in this case i need to see your group query request (the groupby ) that you created already.
meanwhile i can imagine your need, so you have several solution.
the first one is what are you trying to do , create several query that fill your classes ,i can also imagine with Redundancy, that because there are some id who are unfortunately duplicated due to your database structure, in this case you need to use DISTINCT or INTERSECT (even if its not your need here ) .
seconds one you can deal with temporary table using the keyword INTO#tmpTable if you are using sqlserver, so basically is to crate a new tables in memory to perform your query ( those tables is what i propose above ).
try to edit your question with those query you describe.
I am using .NET Core 2.0
I do have 2 tables:
Movies
ID | Name | AuthorId
----------------------
1 | AAAA | 1
2 | BBBB | 1
3 | CCCC | 1
4 | DDDD | 2
Reviews
ID | FilmId | AuthorId | SomeText
------------------------------------
1 | 1 | 1 | this is horror
2 | 1 | 2 | this is comendy
3 | 3 | 1 | it may be horror
4 | 3 | 2 | it is definitely comedy, not horror
My Models are:
class Movie {
[Key]
public int ID { get; set; }
public string Name { get; set; }
public int AuthorId { get; set; }
public List<Review> Reviews { get; set; }
}
class Review {
[Key]
public int ID { get; set; }
public string SomeText { get; set; }
public int AuthorId { get; set; }
public int FilmId { get; set; }
public Movie Film { get; set; }
}
My Context is:
public class MyContext : DbContext
{
public DbSet<Movie> Movie { get; set; }
public DbSet<Review> Review { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Movie>().HasMany(m => m.Reviews).WithOne(r => r.Film);
}
}
Now I want to make such query:
SELECT * FROM Movies m JOIN Reviews r ON (m.ID == r.FilmId) WHERE
m.AuthorId = 1 AND (r.AuthorID = 3 OR r.SomeText LIKE '%definitely%');
The expected result should be CCCC.
Currently I do have:
List<Movie> movies = _context.Movie.Include(m => m.Reviews.Select(r => r.AuthorId == 3 || r.SomeText.Contains("definitely"))).ToList();
Howto express above query with this notation?
However I do get:
InvalidOperationException: The property expression 'm => {from Review
r in m.Reviews select (([r].AuthorId == __user_ID_0) OrElse
[r].SomeText.Contains("definitely"))}' is not valid. The expression
should represent a property access: 'r => r.MyProperty'. For more
information on including related data, see
http://go.microsoft.com/fwlink/?LinkID=746393.
I have read this article but it gave me nothing.
I think this linq query returns the correct result (since I cannot test it)
List<Movie> movies = _context.Movie.where(m => m.AuthorId == 1 && m.Reviews.Any(r =>
r.AuthorId == 3 || r.SomeText.Contains("definitely"))).ToList();
I am stuck in logic to group by. I have a model that has all information but I have to group information according to CustomerBuildingMapping
public class TicketsDataModel
{
public string BuildingID { get; set; }
public string Ticket { get; set; }
public string Amount { get; set; }
public string CustomerID { get; set; }
public string BuildingName { get; set; }
}
Current Data
BuildingID | Ticket | Amount |CustomerID | BuildingName
10 | 001 | 50 | 1 | JP Building
11 | 002 | 45 | 1 | Tiskon
52 | 452 | 35 | 2 | Lalit
65 | 568 | 78 | 2 | Tuilp
41 | 121 | 12 | 1 | BK Trp
-
public class CustomerBuildingMapping
{
public long LeadID { get; set; }
public string CustomerID { get; set; }
public List<BuildingInfo> BuildingInfo{ get; set; }
}
public class BuildingInfo
{
public string BuildingID { get; set; }
public string TicketNumber { get; set; }
public long Amount { get; set; }
public string BuildingName { get; set; }
}
Expected Data after group by
LeadID 1001
CustomerID 1
BuildingInfo
BuildingID | Ticket | Amount | BuildingName
10 | 001 | 50 | JP Building
11 | 002 | 45 | Tiskon
41 | 121 | 12 | BK Trp
LeadID 1002
CustomerID 2
BuildingInfo
BuildingID | Ticket | Amount | BuildingName
52 | 452 | 35 | Lalit
65 | 568 | 78 | Tulip
I have written this code but not able to group by for multiple columns.
List<CustomerBuildingMapping> objCustomerBuildingMappingResult = objTicketsForTheDayInfo.TicketsForTheDay.GroupBy(l => l.CustomerID).Select(grp => new CustomerBuildingMapping
{
CustomerID = grp.Key,
//BuildingInfo = grp.Select(l => l.BuildingID).ToList(),
}).ToList();
You do not need to group by multiple columns. Based on sample data you are only grouping by one field, CustomerID.
var objCustomerBuildingMappingResult = objTicketsForTheDayInfo.TicketsForTheDay
.GroupBy(l => l.CustomerID)
.Select(grp => new CustomerBuildingMapping
{
CustomerID = grp.Key,
LeadId = long.Parse(grp.Key) + 1000,
BuildingInfo = grp.Select(l => new BuildingInfo {
BuildingID = l.BuildingID,
TicketNumber = l.Ticket,
Amount = l.Amount,
BuildingName = l.BuildingName
}).ToList(),
}).ToList();
As Nkosi has pointed out, in OP's example, because LeadId is an automatically generated surrogate key, there's no need to GroupBy on anything other than a single key field (CustomerID), and make a function to generate the surrogate LeadId.
However, in the more general case, if a composite key needs to be constructed for a GroupBy, then both types of Tuples (System.Tuple on older versions of C#, and System.ValueTuple in C#7 and later) make for good transient grouping keys when used with .GroupBy and .ToDictionary. This is because Tuples internally build the tuple instance's HashCode by combining the underlying Hashcodes of the contained types.
For the older System.Tuple, you need to deal with the ugly Itemx properties:
var objCustomerBuildingMappingResult = objTicketsForTheDayInfo
.TicketsForTheDay
.GroupBy(l => Tuple.Create(l.CustomerID, l.BuildingID))
.Select(grp => new CustomerBuildingMapping
{
CustomerID = grp.Key.Item1,
BuildingId = grp.Key.Item2,
// ...
})
.ToList();
But this is more readable (and performant) with System.ValueTuple:
var objCustomerBuildingMappingResult = objTicketsForTheDayInfo
.TicketsForTheDay
.GroupBy(l => (CustomerId: l.CustomerID, BuildingId: l.BuildingID))
.Select(grp => new CustomerBuildingMapping
{
CustomerID = grp.Key.CustomerId,
BuildingId = grp.Key.BuildingID,
// ...
I need advice about my application.
First of all, I have two models which represent two tables (there are not the real tables)
CUSTOMERS (135K rows | 40 columns)
+-------------+--------+------------+
| CUSTOMER_ID | NAME | FIRST_NAME |
+-------------+--------+------------+
| 1234 | DUPONT | JEAN |
+-------------+--------+------------+
ORDERS
+-------------+----------+-------+
| CUSTOMER_ID | ORDER_ID | TYPE |
+-------------+----------+-------+
| 1234 | 5678 | MEET |
| 1234 | 9105 | CANDY |
| 2568 | 7523 | CANDY |
+-------------+----------+-------+
I want to get a customer with his list of orders.
So I created a Viewmodel :
public class ViewModel
{
public string CustomerID { get; set; }
public string Name { get; set; }
public string FirstName { get; set; }
public List<OrdersViewModel> Orders { get; set; }
public ViewModel(){Orders = new List<OrdersViewModel>();
}
And now the query :
var query = from c in northwind.CUSTOMERS
select new ViewModel()
{
CustomerID = c.CustomerID,
Name = c.Name,
FirstName = c.FirstName
};
var CustomersModels = query.ToList();
var queryOrders = from c in northwind.CUSTOMERS
join o in northwind.ORDERS
on c.CustomerID equals o.CustomerID
select new OrdersViewModel()
{
CustomerID = d.CustomerID,
OrderId= d.OrderId,
Type= d.Type
};
var modelsOrders = queryOrders .ToList();
foreach (ViewModel item in modelsOrders )
{
var listModels = modelsOrders .Where(e => e.PMRQTOTM == item.PMRQTOTM).ToList();
item.Orders = listModels;
}
Is there a better way to realise this?
I get the CustomerModel in less than 30 seconds but for the order it takes several minutes...
If Customer and Order has a relationship, you can use Include in first query.
Even then you should not query 135K all at once. Instead, you want to use Skip and Take based on Page Size and Page Index.
Oy, just create the appropriate indexes on your tables and your queries will fly:
CREATE UNIQUE INDEX CUSTOMERS_CUSTOMER_ID
ON CUSTOMERS (CUSTOMER_ID)
CREATE INDEX ORDERS_CUSTOMER_ID
ON ORDERS (CUSTOMER_ID)
In my C# application (an ASP.NET MVC5 website) I am using Entity Framework 6 as my ORM and I'm using code-first with an existing database. The database is SQL Server 2012 Standard.
In my database I have a view (dbo.vBoardReportLayouts) that functions as a joining table for a number of different elements. I've created an entity for this view and configured the navigation properties.
When I attempt to use this entity with LINQ entity framework generates a SQL query that tries to use a table that doesn't actually exist. Specifically it is looking for the BoardReportLayoutPages table.
Now for all the gory details...
The vBoardReportLayouts view looks like this:
| ReportID | PageTypeId | PageId | LayoutID |
+----------+------------+--------+----------+
| 1 | 1 | 1 | 1 |
| 1 | 1 | 1 | 2 |
| 1 | 1 | 2 | 3 |
| 1 | 1 | 3 | 4 |
| 1 | 2 | 4 | 5 |
| 1 | 2 | 4 | 6 |
| 2 | 1 | 1 | 1 |
| 2 | 1 | 1 | 2 |
| 2 | 1 | 2 | 3 |
| 2 | 1 | 3 | 4 |
| 2 | 2 | 4 | 5 |
| 2 | 2 | 4 | 6 |
The BoardReportLayouts entity class:
[Table("vBoardReportLayouts")]
public partial class BoardReportLayout {
public BoardReportLayout() {
BoardReports = new HashSet<BoardReport>();
PageTypes = new HashSet<PageType>();
Pages = new HashSet<Page>();
PageLayouts = new HashSet<PageLayout>();
}
[Key]
public int BoardReportLayoutId { get; set; }
public int BoardReportId { get; set; }
public int PageTypeId { get; set; }
public int PageId { get; set; }
public int PageLayoutId { get; set; }
public virtual ICollection<BoardReport> BoardReports { get; set; }
public virtual ICollection<PageType> PageTypes { get; set; }
public virtual ICollection<Page> Pages { get; set; }
public virtual ICollection<PageLayout> PageLayouts { get; set; }
}
From the OnModelCreating() method of the dbContext:
modelBuilder.Entity<BoardReportLayout>()
.HasMany(e => e.BoardReports)
.WithMany(e => e.BoardReportLayouts);
modelBuilder.Entity<BoardReportLayout>()
.HasMany(e => e.PageTypes)
.WithMany(e => e.BoardReportLayouts);
modelBuilder.Entity<BoardReportLayout>()
.HasMany(e => e.Pages)
.WithMany(e => e.BoardReportLayouts);
modelBuilder.Entity<BoardReportLayout>()
.HasMany(e => e.PageLayouts)
.WithMany(e => e.BoardReportLayouts);
The BoardReport, PageType, Page, and PageLayout entities all have the following property defined:
public virtual ICollection<BoardReportLayout> BoardReportLayouts { get; set; }
Executing this LINQ query:
var test = (from l in ppdb.BoardReportLayouts
where l.BoardReportId == 8
select l.Pages).SelectMany(p => p).ToList();
Generates this SQL:
SELECT [Join1].[PageId] AS [PageId],
[Join1].[PageTypeId] AS [PageTypeId],
[Join1].[Name] AS [Name],
[Join1].[ParameterValue] AS [ParameterValue],
[Join1].[SsrsPageParamterValue] AS [SsrsPageParamterValue],
[Join1].[TableOfContentsOrder] AS [TableOfContentsOrder]
FROM [dbo].[vBoardReportLayouts] AS [Extent1]
INNER JOIN (
SELECT [Extent2].[BoardReportLayout_BoardReportLayoutId] AS [BoardReportLayout_BoardReportLayoutId],
[Extent3].[PageId] AS [PageId],
[Extent3].[PageTypeId] AS [PageTypeId],
[Extent3].[Name] AS [Name],
[Extent3].[ParameterValue] AS [ParameterValue],
[Extent3].[SsrsPageParamterValue] AS [SsrsPageParamterValue],
[Extent3].[TableOfContentsOrder] AS [TableOfContentsOrder]
FROM [dbo].[BoardReportLayoutPages] AS [Extent2]
INNER JOIN [dbo].[Pages] AS [Extent3] ON [Extent3].[PageId] = [Extent2].[Page_PageId]
) AS [Join1] ON [Extent1].[BoardReportLayoutId] = [Join1].[BoardReportLayout_BoardReportLayoutId]
WHERE 8 = [Extent1].[BoardReportId]
The problem is in the FROM clause of the [Join1] table expression. I'm not sure why it is trying to use the table dbo.BoardReportLayoutPages since it could join directly from the view to the Pages table. The table dbo.BoardReportLayoutPages doesn't exist so this throws an exception.
This bit of code is specifying that the BoardReportLayout has a many to many relationship with pages:
modelBuilder.Entity<BoardReportLayout>()
.HasMany(e => e.Pages)
.WithMany(e => e.BoardReportLayouts);
That means there should be a BoardReportLayoutPages table to model that relationship. The BoardReportLayout is supported by a view, so you had to write the sql to create it yourself, because code first would have generated tables to support the model you have defined here. If you do have a many-to-many relationship here, then you should have created the BoardReportLayoutPages as a table or view also.
But you also have a PageId property in the BoardReportLayout. This means that there is a second one-to-many relationship between a Page and BoardReportLayouts. If you want to get to a BoardReportLayout's Page via this relationship then you should add a public Page Page { get; set; } navigation property.
Only you can decide whether you have two relationships here, or just one, and what type of relationship(s) you have.