llblgen Pro SELECT TOP (n) equivalent query - llblgenpro

Is it possible to perform a select top (n) query in llblgen pro?...not LINQ. If so, how can I achieve that using the SelfServicing?

Just use the GelMulti overloads that accepts the maxItemsToReturn parameter:
int itemsToReturn = 10;
OrderCollection orders = new OrderCollection();
orders.GetMulti(null, itemsToReturn);
This is the generated SQL:
SELECT TOP(#p2) [Northwind].[dbo].[Orders].[CustomerID] AS [CustomerId], ...
FROM [Northwind].[dbo].[Orders]
Parameter: #p2 : Int64. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 10.

Related

Oracle Apex get Multiple results from checkboxes

DB = Oracle 11g
Apex = 4.2.6
In the form I have various items which all work great. However I now have a set of check boxes(:P14_DAYS) one for each day of the week.
What I need to do is get all records between :P14_START_DATE :P14_END_DATE, but only within the days select that's checked.
Below is also a sample of the DATE_SETS table
http://i.stack.imgur.com/YAckN.png
so for example
dates 01-AUG-14 - 5-AUG-14 But only require Sundays AND Mondays date would bring back 2 refs.
BEGIN
UPDATE MD_TS_DETAIL
SET job_for = :P14_JOBFORTEM,
job_type_id = :P14_JOB_TYPE_VALUE,
account_id = :P14_ACC_VAL,
qty = :P14_HRS,
rio = :P14_RIO,
post_code = :P14_POSTCODE
WHERE id IN (SELECT D.id
FROM MD_TS_MAST M
LEFT JOIN MD_TS_DETAIL D
ON M.mast_id = D.md_id
LEFT JOIN DATE_SETS
ON ms_date = dt
WHERE eng_id = :P14_ENG_VAL
AND ms_date BETWEEN :P14_START_DATE AND :P14_END_DATE
AND DATE_SETS.col_day = ANY instr(':'||:P14_DAYS||':',Return)
END;
Any help would be much appreciated .
I found this example: http://docs.oracle.com/cd/B31036_01/doc/appdev.22/b28839/check_box.htm#CHDBGDJH
As I can understand, when you choose some values in your checkbox list, item :P14_DAYS receives value, that contains return values of chosen elements of the LOV with delimiters. Then you need to replace this string in your query
AND DATE_SETS.col_day = ANY instr(':'||:P14_DAYS||':',Return)
with
AND instr(':'||:P14_DAYS||':', ':'||DATE_SETS.col_day||':') > 0
Here function instr searches substring DATE_SETS.col_day in string :P14_DAYS. It returns position of substring if substring was found or 0, if not found. Then you compare result of function with 0, and if result > 0, it means that DATE_SETS.col_day is among selected values.

Find Minimum and Maximum of a column in LInq query result

I have an Linq query like this:
var result = from c in displayedEmployees select new[] { c.Name, c.Code, c.Rank.ToString(), c.Id.ToString(),c.Rank. };
If i want to select the min and max of any column for example Rank along with this like:
var result = from c in displayedEmployees select new[] { c.Name, c.Code, c.Rank.ToString(), c.Id.ToString(),min(c.Rank) ,max(c.Rank)};
how can I do this?
Select actually performs a transformation of every item (displayedEmployee), so ends up with the same number of items as you started with. Min and max are reductions, they reduce the list into a scalar.
After the "select" keyword is a function that only recieves the current item (c in your case). So there is no way to get the max there and you don't want every item in result to repeat the same maximum value.
You should compute the minimum and maximum separately.
After:
var result = from c in displayedEmployees select { c.Name, c.Code, c.Rank };
You could do something like this:
var min = result.Min(x => x.Rank);
var max = result.Max(x => x.Rank);
See more samples here (LINQ 101): http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

Linq to Entities Percentages

Does anyone have any tips for calculating percentages in Linq to Entities?
I'm guessing that there must be a more efficient way than returning 2 results and calculating in memory. Perhaps an inventive use of let or into?
EDIT
Thanks Mark for your comment, here is a code snippet, but I think this will result in 2 database hits:
int passed = (from lpt in this.PushedLearnings.Select(pl => pl.LearningPlanTask)
where lpt.OnlineCourseScores.Any(score => score.ActualScore >= ((lpt.LearningResource.PassMarkPercentage != (decimal?)null) ?lpt.LearningResource.PassMarkPercentage : 80))
select lpt).Count();
int total = (from lpt in this.PushedLearnings.Select(pl => pl.LearningPlanTask)
select lpt).Count();
double percentage = passed * 100 / total;
If you use LINQ to Entities and write something along the lines of select x * 100.0 / y in your query then this expression will be converted to SQL and run in the database. It will be efficient.

How to query and calculate dates in the where clause of a LINQ statement?

I am having trouble with the following piece of code. Before I paste it, Let me give a bit of history on what should happen.
I have a model containing 2 fields of interest at the moment, which is the name of the order the customer placed, and the date at which he/she placed it. A pre-calculated date will be used to query the dateplaced field (and should only query the dates , and not the time). The query counts the amount of duplicates that occur in the MondayOrder field, and groups them together. Now , when I exclude the where clause which should query the dates, the query runs great. However, The goal of this query is to count the amount of orders for the following week based on the date the order has been placed.
List<string> returnlist = new List<string>();
DateTime dt = getNextWeekMondaysDate().Date;
switch (day)
{
case DayOfWeek.Monday:
{
var CountOrders =
from x in Data.EntityDB.Orders
group x by x.MondayOrder into m
let count = m.Count()
select new
{
MondayOrderItem = m.Key, Amount = count
};
foreach (var item in CountOrders)
{
returnlist.Add(item.MondayOrderItem + " : " +
item.Amount);
}
}
break;
The getNextWeekMondaysDate() method has an overload which I can use, where if I supply it a date, it will get the following Monday's date from the parameter given. The problem is though, LINQ does not accept queries such as the following:
var CountOrders =
from x in Data.EntityDB.Orders
where getNextWeekMondaysDate(x.DatePlaced.Value).Date == dt
group x by x.MondayOrder into m
let count = m.Count()
select new { MondayOrderItem = m.Key, Amount = count };
This is exactly what I must achieve. Is there any workaround for this situation?
UPDATE
Here is the exception I get when I try the 2nd query.
LINQ to Entities does not recognize the method 'System.DateTime getNextWeekMondaysDate(System.DateTime)' method, and this method cannot be translated into a store expression.
You cannot do this directly, as user-defined method calls cannot be translated to SQL by the EF query provider. The provider recognizes a limited set of .NET methods that can be translated to SQL and also a number of canonical functions as well. Anything that cannot be expressed using these methods only is off-limits unless you write your own query provider (which is only theoretically an option).
As a practical workaround, you can calculate an appropriate range for x.DatePlaced.Value in code before the query and then use specific DateTime values on the where clause.
As an intellectual exercise, note that this method is recognized by the query provider and can be used as part of the expression. So this abomination should work too:
var CountOrders =
from x in Data.EntityDB.Orders
where EntityFunctions.AddDays(
x.DatePlaced.Date.Value,
(9 - DateAndTime.DatePart(DateInterval.WeekDay, x.DatePlaced.Value)) % 7)
.Date == dt
group x by x.MondayOrder into m
let count = m.Count()
select new { MondayOrderItem = m.Key, Amount = count };
Linq to Entities doesn't know how to convert arbitrary C# methods into SQL - it's not possible in general.
So, you have to work with the methods it does understand.
In this case, you could do something like this:
DateTime weekBegin = CalculateWeekBegin( dt );
DateTime weekEnd = CalculateWeekEnd( dt );
var CountOrders =
from x in Data.EntityDB.Orders
where x.DatePlaced.Value >= weekBegin && x.DatePlaced.Value < weekEnd
group x by x.MondayOrder into m
let count = m.Count()
select new { MondayOrderItem = m.Key, Amount = count });

LINQ Conditional Sum Calculation - cannot be applied to operands of type 'int' and 'bool'

I am trying to perform to calculation. I have a donations (d) table that contains Quantity Needed (d.QtyNeeded) and I need to determine the numbers of items still needed by pulling quantity filled (qtyfilled) from the donors table. Not every donation has a donor, so I need a conditional statement to handle nulls so the sum will work. When I try to compile, I am getting an error: *Operator '-' cannot be applied to operands of type 'int' and 'bool'. I am not great at Linq yet, what am I missing or is there a better way?
QtyOpen = d.QtyNeeded - (from dv in db.Donors
select dv).All(v => v.QtyFilled == null)
? 0
: (from dv in db.Donations
select dv.QtyFilled).Sum()
The problem is not the LINQ statement, but rather precedence in the subtraction operator. Consider this example:
int result = quantity - true ? 0 : otherValue;
This fails to compile for the exact same reason, Operator '-' cannot be applied to operands of type 'int' and 'bool'. The solution is to simply group the conditional statement in parens:
int result = quantity - (true ? 0 : otherValue);
So, your example should compile by adding parens around your entire conditional operator statement.
Try filtering out the nulls within the initial select by adding a where in the select:
QtyOpen = d.QtyNeeded - (from dv in db.Donors
where dv.QtyFilled != null
select dv.QtyFilled).Sum();

Resources