JuliaDB groupby many columns in one time - group-by

I’m trying to groupby using JuliaDB , everything works well except when i want to apply a function that takes two columns in one time.
JuliaDB.groupby(
(
TOTAL_PAID = :PRICE => sum,
FREQUENCY_IN_DAYS = :ORDER_DATE => handle_dates,
RECENCY_IN_DAYS = :ORDER_DATE => (x -> Dates.value(Date(now()) - maximum(x))),
COUNT_ORDERS_LAST_MONTH = :ORDER_DATE => countOnlyLastMonth,
TOTAL_PAID_LAST_MONTH = [:ORDER_DATE, :PRICE] => countOnlyLastMonth,
COUNT_ORDERS = length
),
t, :USER_KEY)
It’s this line that doesn’t work
TOTAL_PAID_LAST_MONTH = [:ORDER_DATE, :PRICE] => countOnlyLastMonth,
With DataFrames.groupby you would do it as following :
[:ORDER_DATE, :PRICE] => countOnlyLastMonth => :TOTAL_PAID_LAST_MONTH,
How can i do that with juliaDB ? Thank you

Related

Entity Framework Core: combining multiple result objects into one collection

I am facing this linq query for entity framework core (2.0).
var result = await dbContext.Table1DbSet
.Where(t1e => t1e.Id == id && t1e.Id2 == id2)
.Select
(
t1e =>
t1e.Table2NavPropICollection.Select
(
t2e => new
{
singleObject = t2e.Table3NavPropObject.TargetObject,
enumerable1 = t2e.Table3NavPropObject.Table4NavPropObject.Table5NavPropICollection.Select(t5e => t5e.TargetObject),
enumerable2 = t2e.Table3NavPropObject.Table6NavPropObject.Table7NavPropICollection.Select(t7e => t7e.TargetObject),
enumerable3 = t2e.Table3NavPropObject.Table8NavPropICollection.SelectMany(t8e => t8e.Table9NavPropICollection.Select(t9e => t9e.TargetObject))
}
)
)
.ToListAsync();
The goal is to query all the referenced instances of TargetObject which is referenced accros ~ 10 different tables.
Currently it returns an IEnumerable where the anonymous object contains the properties singleObject, enumerable1, enumerable2, enumerable3. All those properties are of type TargetObject or IEnumerable.
Can I, and how, rewrite the query to not return anonymous objects but just an IEnumerable containing all the values?
For some reason the compiler won't let me iterate over the anonymous collection and flatten it manually.
This should do the trick if you want one array per t1e row.
var result = await dbContext.Table1DbSet
.Where(t1e => t1e.Id == id && t1e.Id2 == id2)
.Select
(
t1e =>
t1e.Table2NavPropICollection.Select
(
t2e => new[] {t2e.Table3NavPropObject.TargetObject}.Concat(
t2e.Table3NavPropObject.Table4NavPropObject.Table5NavPropICollection.Select(t5e => t5e.TargetObject)).Concat(
t2e.Table3NavPropObject.Table6NavPropObject.Table7NavPropICollection.Select(t7e => t7e.TargetObject)).Concat(
t2e.Table3NavPropObject.Table8NavPropICollection.SelectMany(t8e => t8e.Table9NavPropICollection.Select(t9e => t9e.TargetObject)))
)
)
.ToListAsync();
If you want it completely flat, you'll want to switch that Select to a SelectMany().
var result = await dbContext.Table1DbSet
.Where(t1e => t1e.Id == id && t1e.Id2 == id2)
.SelectMany
(
t1e =>
t1e.Table2NavPropICollection.Select
(
t2e => new[] {t2e.Table3NavPropObject.TargetObject}.Concat(
t2e.Table3NavPropObject.Table4NavPropObject.Table5NavPropICollection.Select(t5e => t5e.TargetObject)).Concat(
t2e.Table3NavPropObject.Table6NavPropObject.Table7NavPropICollection.Select(t7e => t7e.TargetObject)).Concat(
t2e.Table3NavPropObject.Table8NavPropICollection.SelectMany(t8e => t8e.Table9NavPropICollection.Select(t9e => t9e.TargetObject)))
)
)
.ToListAsync();
Is Add and/or Concat a possible solution?
PoC:
var a = new [] { 1,2,3 };
var result = new {
firstA = a.First(),
otherAs = a,
backwards = a.Reverse()
};
var final = new List<int>();
final.Add(result.firstA);
final.Concat(result.otherAs.Concat(result.backwards))
.Dump();

Select performance problems in EF Core

_context.Homestays
.Include(x => x.CreatedUser)
.Include(x => x.UpdatedUser)
.Include(x => x.HomestayEvaluations)
.Include(x => x.HomestayContracts)
.Include(x => x.HomestayPoliceChecks)
.Include(x => x.HomestayHouseHolds)
.AsNoTracking()
.Select(x => new Homestay()
{
HomestayId = x.HomestayId,
HomestayFamily = ConstValue.GetHomestayFamilyName(x),
Address = x.Address,
Score = x.HomestayEvaluations.Any(x1 => x1.IsEvaluationActive) ? x.HomestayEvaluations.LastOrDefault(x1 => x1.IsEvaluationActive).GetScore() : 0,
Contract = x.HomestayContracts.Any(x1 => x1.IsContractActive) ? x.HomestayContracts.LastOrDefault(x1 => x1.IsContractActive).ContractDate : null,
Students = x.Students,
HouseHolders = x.HomestayHouseHolds.Count(x1 => x1.IsHouseHoldActive),
PoliceCheck = x.HomestayPoliceChecks.Any(x1 => x1.IsPoliceCheckActive) ? x.HomestayPoliceChecks.LastOrDefault(x1 => x1.IsPoliceCheckActive).PoliceCheckDate : null,
Language = x.Language,
Room = x.Room,
IsActive = x.IsActive,
CreatedDate = x.CreatedDate,
CreatedUserName = ConstValue.GetUserName(x.CreatedUser),
UpdatedDate = x.UpdatedDate,
UpdatedUserName = ConstValue.GetUserName(x.UpdatedUser)
})
.OrderByDescending(x => x.HomestayId);
Hello, Im wonder that how do I change my select query better ?
This code as below that it looks messy.
first excute any and if it is true, get last one. but is there any more short code ??
Contract = x.HomestayContracts.Any(x1 => x1.IsContractActive) ? x.HomestayContracts.LastOrDefault(x1 => x1.IsContractActive).ContractDate : null
I tried without any(), it occred error as null object if there is no data.
Please help me.
Thanks :)
Use this
Contract = x.HomestayContracts.LastOrDefault(x1 => x1.IsContractActive)?.ContractDate ?? null
Also you can disable lazy loading for this query in context's configuration
context.Configuration.LazyLoadingEnabled = false;
This way you can eliminate Include methods, cause shorter statement but not better performance. Use stored procedure for performance. Stored Procedure in Entity Framework

Converting a datetime to date in iEF

I have this Linq statement that runs as expected in LinqPad (using Linq to SQL) but when I bring it into my C# app using EF 6, I get an error saying that cd.LogTimestamp.Date (in the GroupBy statement) is not supported. I'm attempting to convert a datetime2 value to a date value for group by purposes.
helper.GetDbContext().SiteLog
.Where( sl => sl.SiteId == 3 )
.GroupBy( cd => new { cd.SiteId, cd.LogTimestamp.Date } )
.Select( g => new DailyTraffic()
{
SiteId = g.Key.SiteId,
TrafficDate = g.Key.Date,
NewUsers = g.Count( row => row.IsNewUser ),
ReturningUsers = g.Count( row => row.IsReturningUser ),
TotalUsers = g.Count( row => row.IsNewUser ) + g.Count( row => row.IsReturningUser ),
PageViews = g.Count( row => row.IsPageView )
} )
.OrderBy( g => g.SiteId ).ThenBy( g => g.TrafficDate )
.ToList();
Is there a preferred way to convert a datetime2 value to a date value in EF 6
Using this QA ( how to use entity framework to group by date not date with time ) as a guide, you'll want to use EntityFunctions so you can use built-in SQL Server functions directly, this does of course mean your application won't be immediately portable to other databases.
.GroupBy( cd => new { cd.SiteId, EntityFunctions.TruncateTime( cd.LogTimestamp ) } )

Query that get the projects whose name 'starts with'. Project Server 2013

Attempt to obtain projects that begin with a particular word , but I get the following error: "The 'StartsWith' member cannot be used in the expression."
ProjectContext projContext = new ProjectContext(urlPWA);
projContext.Credentials = new SharePointOnlineCredentials(user,passwordSecurity);
projContext.Load(projContext.Projects, c => c.Where(p => p.Name.StartsWith(name, true, new CultureInfo("es-ES"))).IncludeWithDefaultProperties(f => f.Name, f => f.Tasks, f => f.ProjectResources, f => f.Owner.UserId, f => f.CheckedOutBy.UserId));
projContext.ExecuteQuery();
I'm not too familiar with special queries like that. But a quick workaround would probably be to get the whole collection and iterate it afterwards. Hopefully you do not have a million projects in your PWA :)
projContext.Load(projContext.Projects);
projContext.ExecuteQuery();
foreach (PublishedProject pubProj in projContext.Projects)
{
if (pubProj.Name.StartsWith("yourString") {
// Do something
}
}

Is it possible to conditionally pass options to a method in perl?

Similar to this question regarding Ruby, I'd like to conditionally pass parameters to a method. Currently, I have it configured as follows:
my $recs = $harvester->listAllRecords(
metadataPrefix => 'marc21',
metadataHandler => 'MARC::File::SAX',
set => 'pd',
from => $from,
until => $until,
);
What I'd like is to be able to conditionally pass the from and/or until parameters, depending on previous code. This is not syntactically correct, but something like this:
from => $from if ($from),
until => $until if ($until),
or this:
if ($from) {from => $from,}
if ($until) {until => $until,}
Is this possible, and if so, how would I go about doing it?
You could use the ternary ?: operator with list operands:
my $recs = $harvester->listAllRecords(
metadataPrefix => 'marc21',
metadataHandler => 'MARC::File::SAX',
set => 'pd',
$from ? (from => $from) : (),
$until ? (until => $until) : (),
);
It may also be worth knowing about the "conditional list include" pseudo-operator, which in this case would work like
...
(from => $from) x !!$from,
(until => $until) x !!defined($until),
...
but the ternary operator expression is probably easier to read for most people.
The other option is to build a list (or hash) of args and then call the method:
my %args = (
metadataPrefix => 'marc21',
metadataHandler => 'MARC::File::SAX',
set => 'pd',
);
$args{from} = $from if $from;
$args{until} = $until if $until;
my $recs = $harvester->listAllRecords(%args);