OData groupby / filter on field of derived type - group-by

Let's assume I have the following data model:
[Person]
Id - number
FirstName - string
LastName - string
[Director : Person]
YearlyIncome - number
[Employee : Person]
Age - number
[File]
Id - number
CreatedAt - date
AssignedTo - Person
As we can see the File entity has a field called AssignedTo which is a Person.
Now let's say I want to count the amount of Files by the FirstName of the Person, this works fine, because FirstName is a field the Person entity. The query would look like this, for example:
files?$apply=groupby((assignedTo/firstName),aggregate($count as count))
But now what I want is to be able to group by Age, a field on the Employee entity.
However if I use the following query:
files?$apply=groupby((assignedTo/age),aggregate($count as count))
I get the following error:
Could not find a property named 'age' on type 'App.Core.Entities.Person'.
According to OData Version 4.0 docs it should be possible (see 4.9).
I managed to get the query below to work, to select a field of a derived type:
/files?$expand=assignedTo($select=App.Core.Entities.Employee/Age)
But I can't apply this when using groupby, here's what I've tried:
$apply=groupby((assignedTo($select=App.Core.Entities.Employee/Age)),aggregate($count as count))
$apply=groupby((assignedTo/App.Core.Entities.Employee/Age)),aggregate($count as count))
$apply=groupby((assignedTo/App.Core.Entities.Employee/Age)),aggregate($count as count))
I guess the error makes sense, but I'm wondering what's the best way to solve this.

Related

How to know Mongo Collection Data Type?

In Mongo collection data is stored as json with different data type but I want to check all schema bsontype without mentioning the attribute.
that gives the result like - attribute1 - int, attribute2 - string , attribute - bool

Spring data jpa - compicated query

I am trying to prepare compliacted query using Spring Data JPA.
Page<Line> findByIdIn_AndNumberIgnoreCaseContainingAndActualTrueAndOrNameIgnoreCaseContainingInAndActualTrueOrderById(List<Long> idList, String number, String name, Pageable pageable);
My goal is to build query like:
select * from line where id in (?,?,?) and (number like ? and actual = true) or (name like ? and actual = true) order by id
Unfortunately I have following error:
No property in found for type Long! Traversed path: Line.id.
Whats wrong with my method name?
And also I made a method:
Page<Line> findByNumberIgnoreCaseContainingAndActualTrueOrNameIgnoreCaseContainingAndActualTrueOrderById(String number, String name, Pageable pageable);
And that one works good. The problem is with id in..

Generated SQL query with "WHERE (1 <> 1)" condition

I'm trying to query a many-to-many relationship using the Gorm ORM for Go.
I have two structs: User & Address.
type User struct {
// gorm.Model
UUID string `gorm:"type:uuid;primary_key;auto_increment:false"`
Firstname string
// ...
Addresses []Address `gorm:"many2many:useraddresses"`
}
// Address represents the Postgres SQL address model
type Address struct {
UUID string `gorm:"type:uuid;primary_key;auto_increment:false"`
Line1 string
// ...
}
I took inspiration from the many-to-many example shown here in the documentation, (except I used a slice of users []User instead of a single user).
var u []User
var a []Address
If I query just using the users as a Model, all users are returned (sends sql query SELECT * FROM "users"):
db.Model(&u).Find(&u)
However, if I include related Addresses, surgeons are returned, but no Addresses:
db.Model(&u).Related(&a, "Addresses").Find(&u)
This creates another sql query that precedes the first:
SELECT "addresses".*
FROM "addresses" INNER JOIN "useraddresses" ON "useraddresses"."address_uuid" = "addresses"."uuid"
WHERE (1 <> 1)
Of course, the where false condition prevents any addresses from being returned.
Can anyone shed light on how I can include the addresses using the db.Model method of Gorm?

Implement search functionality with Entity Framework

I have three tables
SalesDetails with columns SalesId, ProductId, Qty, Price etc
SalesPersonDtls with columns SalesId, SalesPersonId, CommPercentage etc
SalesPerson with columns SalesPersonId, firstName, lastName etc
I have second table because one sale can be done by more than one sales person together with split commission.
I have various inputs in search screen like productname, sales date, sales person name etc.
I am making the model class as 'AsQueryable' and add various where conditions and finally the result into a list.
I have sales person's name in search criteria but I don't know how to include this into the search. Can you please help?
Thanks
Peter
Peter
If I get it correct , relation of your business models is like this :
person (n) <-----> (1) Sale (1) <-----> (n) Details
you put sale and person relation in "SalesPersonDtls" and sale and detail relation to "SalesDetails". I think it's better to change your entities a little bit, if you want to get better result as your project getting bigger and more complex.
Your entities should be like this :
Sale
{
List<SalesDetail> details;
List<Person> persons;
...
}
SalesDetail
{
Sale
...
}
Person
{
Sale
name
...
}
Now it's really simple , if you want sales that is related to a personName :
sales.Where(sale => sale.Persons.Any(person => person.PersonName == "your input name"));
UPDATE :
If you can't or don't want to change your models:
first you need to find personId by it'name and then search into your "SalesPersonDtls" and get saleIds.

Using IN subquery in Entity Framework using Linq

I have the following tables in my database:
- Reservation
- TrainStation
- Train
- Traveller
- Reservation_Traveller
- TrainSeats: Attributes are: Train_Id, Seat, Traveller_Id
I want to find the TrainSeats rows of a particular Reservation
I have a Reservation object that contains an ICollection<Traveller> property containing the travellers of which I want to remove their seats from the TrainSeats table. I fetched the reservation object from the database like this:
var reservation = db.Reservation.Where(r => r.Id == id).FirstOrDefault();
So I want to do something like this:
db.TrainSeats.Where(ts => ts.Traveller_Id IN reservation.Travellers)
First select the travelers id:
var ids=reservation.Travellers.Select(e=>e.Id);
Then use Contains extension method which is translated into IN in sql:
var query=db.TrainSeats.Where(ts => ids.Contains(ts.Traveller_Id));
I guess if you use the FK property and navigation properties you can do it in one query:
var query= db.Travellers.Where(e=>e.ReservationId==id).SelectMany(t=>t.TrainSeats);