how to write parameter string for include for linq query? - linq

If table Employee has foreign key for table person, department,
when get the entity of employee, I want to both associated entities loaded too.
how to write the linq query? like below?
var employee = this.Context.Employee.Include("Person, Department");
It seems not working.

var employee = this.Context.Employee.Include("Person").Include("Department");

Related

How to back-reference objects in self-referencing many-to-many relationship?

I am building a blog application and one of the features is being able to follow other users. This creates many-to-many relationship between the user which I declared like so:
type User struct {
gorm.Model
Username string
Password string
Followers []*User `gorm:"many2many:user_followers"`
}
When migrating this model, the following join table is created:
join table: user_friends
foreign key: user_id, reference: users.id
foreign key: follower_id, reference: users.id
My question is, how can I retrieve the followings of a user?
To get the followers of a user, I can simply do:
var followers []User
db.Model(&user).Association("Followers").Find(&followers)
But I can't figure out a way to retrieve the followings in a similar manner. I know I can query the join table to get the followings but this means that for each object thats returned from this query, I'd need another query to get the user associated with the user_id. This seems super inefficient at scale.
How can I do this effectively and efficiently?
Thank you!

How to get specific columns from relation table in laravel?

I am trying to fetch soecific columns data from relational table but it is giving me null
$allConsignments = Consignment::query();
$allConsignments->select(['id','customer_reference'])->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get();
When I don't use select() then it gives correct data .
like this
$allConsignments = Consignment::query();
$allConsignments->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get()
it is working but I also need specific columns from Consignment Table. what could be the reason?
You can also do like this.
$allConsignments = Consignment::query();
$allConsignments::with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get(['id','customer_reference']);
Actually, I also need to select the foreign key column from the table on which relationship is based. for example in my case I have customer_id in consignment table so it should be like that
$allConsignments = Consignment::query();
$allConsignments->select('id','customer_reference','customer_id')->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get();
I need to select customer_id as well

Hibernate - How to get single value from another table without primary/Foreign key relationship?

I have 2 tables(Employee & Department).
Employee(ID, employee name, dept id, employee address) --> ID primary key
Department(ID, department name, address) --> ID primary key
Note: Consider no primary key / foreign key relationship between these 2 tables
Now based on dept id in Employee, i need to fetch its corresponding dept name from Department & display it in Employee.
Employee(ID, employee name, dept id(Display dept name instead of dept id, query department table to get dept name through passing dept id), employee address)
What are all possible ways to achieve this?
Regards
Raj
I see at least three possible solutions:
Create a "normal" SQL query for this, joins can be perfectly done without a PK/FK-relationship.
Perform two SQL queries: One query for fetching the employee, then extract the ID and then a second query to fetch the department by ID.
Change your JPA entities to include the relationship as you would normally do having a #OneToMany relationship so you can load the employee with its department as usual. Same as 1 applies, joins (whether done by you or by the JPA provider) don't need a PK/FK-relationship.
Whilst 1 can be done with plain SQL, 2 can be done using the entity manager without the need to write "plain" SQL. 3 would be the easiest solution, obviously.
You can #Query in repository and write your JPA query using JOIN.
Sample Query:
#Query( value = "select new map(employee.name as empName, employee.id as id, department.id as depId, department.name as depName) from Employee employee INNER JOIN Department department ON employee.depId = department.id where employee.id = :id" )
public Map<String,Object> getEmployeeById(long id);

Creating a linq query which results in only 1 query execution

I have the following two tables:
*tableA
*tableB
TableA has a 1 to many relationship with tableB and therefore TableB has a column with a foreign key for tableA
TableA will be used to create a c# class called classA which has a bool property indicating wether it has related records in TableB called HasRecords
I could create it like this:
TableA.Select(s => new ClassA {
Hasrecords = s.TableB.Any()
}
Will this create 1 query or multiple ones because of the any function? Is there another way to do this in line without triggering multiple query executions or do I have to resort to a table valued function?

Filter out a member from SSRS (MDX)

Ok, So I have a dim called Employee that has Employee_ID and the ID I am trying to filter out from the report table is &[12345].
How would I go about filtering out a record for just that employee ID from my report table?
You can use the Except function:
Except('a set that contains employees', {'the employee you want to exclude'})

Resources