I’m developing an application where I’m using Entity Framework. I have a table A and an autogen entity from this table class A
Public Class A
ID As Integer
Sum As Integer
TotalSum As Integer
LastPayment As Integer
NewPayment As Integer
.
.
.
End Class
In addition to my table I have a view that calculates and returns all the rows from table A where totalSum and LastPayment meets some conditions (table has 50 rows, view returns 35 rows).
Can I use this view together with my entity class A? When I use my entity class A I can say
unitOfWork.ARepository.Filter(Function(p) p.ID = Me._id, , )
but this will get the rows from the table without the calculations/filtering done by the view, let say it returns 50 row. I want to say
unitOfWork.ARepository.Filter(Function(p) p.ID = Me._id, , )
but I want to get the filtered rows from the view instead, this will return 35 rows instead of the 50. But I do not want the view to be an entity in my model, because I then will have two classes A (from table) and B (from view) that looks exactly the same. How can I solve this?
you can write a code corresponding view with entity framework in VB or C#. it's better than use 2 model that are equal.
Related
I created an ABAP CDS view from a dataset. This dataset contains data with a field "OrderID" (from order 1 to order 10000). Based on this field, OrderID, I would like to create 2 query views : one containing only data from order 1 to 20 and another one with order 50 to 70.
Therefore, I was wondering if there is an annotation to select the value I want to show/remove. I don't want to filter for performance reason.
Use a where clause, as described in the ABAP keyword documentation:
define view first_query_view as
select from your_base_view
{ ... }
where OrderID between 1 and 20;
define view second_query_view as
select from your_base_view
{ ... }
where OrderID between 50 and 70;
Annotations explain how views and the elements in them are to be used. They don't control how the data is retrieved, joined, or filtered.
I have a parent table and a child table. The parent table only lists ranges of attributes. I'm looking to merge the two to create a proper hierarchy, but I need a way to filter the child table by the parent range first, I believe.
Here is a sample of the parent table:
parent_item start_attribute end_attribute
A 10 120
B 130 130
C 140 200
And the child table:
child_item child_attribute
U 10
V 50
W 60
X 130
Y 140
Z 150
The output table I'd be looking for is such:
parent_item child_item
A U
A V
A W
B X
C Y
C Z
To further confuse things, the attributes are alphanumeric, which eliminates uses a List.Generate() function I believe. I think I'm looking for something similar to the EARLIER() function in DAX, but I'm not sure I'm even looking at this problem the right way. Here is my pseudo code as I'd see it working:
Table.AddColumn(
#"parent_table",
"child_item",
each
Table.SelectRows(
child_table,
each ([child_attribute] <= EARLIER(end_attribute) and [child_attribute]>= EARLIER(start_attribute) )
)
)
This is a simplification as the child table actually contains five attributes and the parent table contains five respective attribute ranges.
I found this blog post, which held the key to referencing the current row environment. The main takeaway is this:
Each is a keyword to create simple functions. Each is an abbreviation for (_) =>, in which the underscore represents (if you are in a table environment, as we are) the current row.
Using a new function C for child_table, we can write
= Table.AddColumn(#"parent_table", "child_table", each
Table.SelectRows(Child, (C) =>
C[child_attribute] >= [start_attribute] and
C[child_attribute] <= [end_attribute]))
or more explicitly as
= Table.AddColumn(#"parent_table", "child_table", (P) =>
Table.SelectRows(Child, (C) =>
C[child_attribute] >= P[start_attribute] and
C[child_attribute] <= P[end_attribute]))
Once you add this column, just expand the child_item column from your new child_table column.
One possible approach is to do a full cross join and then filter out the rows you don't want.
Create a custom column on both tables with a constant value of, say, 1.
Merge the Child table into the Parent table matching on the new column.
Expand out the Child table to get a table like this:
Create a custom column with all your desired logic. For example,
if [child_attribute] >= [start_attribute] and
[child_attribute] <= [end_attribute]
then 1
else 0
Filter out just the 1 values in this new column.
Remove all other columns except for parent_item and child_item.
I'm using Entity Framework to map database to a class in my application.
The class is automatically generated as partial class by EF and I also added some other properties in my own file to the same partial class.
I use these queries to get a list of entities from the table (they are equivalent as far as I can tell):
db.DailyEntry.ToList
db.DailyEntry.SqlQuery("SELECT * FROM DailyEntry").ToList
db.Database.SqlQuery(Of DailyEntry)("SELECT DailyEntry.DailyEntryId, DailyEntry.Driver, DailyEntry.Billing, DailyEntry.EntryDate FROM DailyEntry").ToList
I then added a field to this class:
Public Property IsHoliday As Boolean = False
I iterated through the list and calculated whether the date falls on a bank holiday. I have a database table of holidays:
For Each entry As DailyEntry in _myList_
entry.IsHoliday = db.Database.SqlQuery(Of Boolean)("SELECT IsHoliday FROM Holiday WHERE HolidayDate = {0}", entry.EntryDate).FirstOrDefault
Next
This all works fine but as the amount of records increased, so did the number of database calls and I need to speed the application up by merging these into a single call.
I modified the query to include the holiday info:
SELECT DailyEntry.*, Holiday.IsHoliday AS IsHoliday FROM DailyEntry LEFT OUTER JOIN Holiday ON DailyEntry.EntryDate = Holiday.HolidayDate;
or
SELECT DailyEntry.DailyEntryId, DailyEntry.Driver, DailyEntry.Billing, DailyEntry.EntryDate, Holiday.IsHoliday AS IsHoliday FROM DailyEntry LEFT OUTER JOIN Holiday ON DailyEntry.EntryDate = Holiday.HolidayDate;
and a few similar tries. They work well when I test them as queries on the database and return expected data, but when I try to use them in my app:
db.Database.SqlQuery(Of DailyEntry)("SELECT DailyEntry.*, Holiday.IsHoliday AS IsHoliday FROM DailyEntry LEFT OUTER JOIN Holiday ON DailyEntry.EntryDate = Holiday.HolidayDate").ToList
the IsHoliday property is always left at default value (False).
Is there a way to get these calculated columns that are not part of the original database table to map to my properties?
Thanks in advance,
Zdenek
I want to get the detail entity of a parent entity with a custom method in this method I want to sort the detail entity random and exclude the details by a condition it's possible in the parent method set the method for get the childs of the parent entity?
In your example I have order and orderdetail like this:
OrderId = 1
date = 2015-06-01
Order detail
Order id = 1
Product = 1
RowNumber = 2
Order detail
Order id = 1
Product = 2
RowNumber = 3
I need that the order details shoul be order by rownumber in a random sort and I want that I get in the order object when I acces to the detail like Order.OrderDetails I get the orderdetails in random I have a method that returns the orderdetail in random but I don't set how set in the graphical design to set my method for get the orderdetails list collection. Other dude I try to add a cfl method for order random something like this in the order detail object
LOAD (int orderId) WHERE orderId = #orderId ORDER BY NEWID()
SELECT * FROM table ORDER BY NEWID()
and get a random order but I get and error so I add a partial class to order by random and add for example an product id like this a)2 b) 1
You can create a CFQL method with inline SQL:
<cf:method name="LoadByOrderRandom"
body="LOAD(Order) where Order = #Order order by [newid()]"
checkLevel="None" />
More information about raw methods: http://blog.codefluententities.com/2014/07/03/cfql-raw-methods/
I have a model in EF4. There are two hierarchical entities in this model that have a 0..1->many relationship with themselves (they have a ParentId field), so each has a Parent (same type) and a Children (collection of same type) property. their definitions are almost the same.
one of them works fine but in the other one, Children property always contains the first child event if it has multiple children.
Update:
I did a test with Sql Server Profiler:
var p = from item in context.Entity1.Include("Children")
where item.Id == 15
select item;
var q = from unit in context.Entity2.Include("Children")
where unit.Id == 239
select unit;
both of these Linq statements generate sql queries that return 3 records because Entity1 with id 15 and Entity2 with Id 239 both have 3 children. q.First().Count returns 1 but p.First().Count returns 3.