Entity Framework - query inside a where statement - entity-framework

I have a scenario where I need to fetch list of string values from a table.
I need to query the Subject table and get list of subjects:
EmployeeDetails emp = new EmployeeDetails();
emp.subjects = from x in EmpDB.subjects
join y in EmpDB.employeeInfo
on x.subjectCode equals y.subjectCode select new
{
x.subjectTitle,
}.toList();
I have an employee ID which I need to query the EmployeeDetails table and get SubjectCodes of that particular Employee ID, and using those subject codes, I need to query the Subject table and get the subject values.

I assume that in EmployeeDetails class you have property Subjects with List.
EmployeeDetails emp = new EmployeeDetails();
emp.subjects = (from x in EmpDB.subjects
join y in EmpDB.employeeInfo on x.subjectCode equals y.subjectCode
where y.EmployeeId = <<your ID>> // Employee ID you searching
select x.subjectTitle).ToList();

Related

How to get value of specific column on first table and insert into the second table in Spring boot JPA

I have a project I want to get data from a specific column from table one and insert those data by id to the specific column in table 2 what query could I use? how to get data from specific column from table 1 to a specific column to table 2
here's my Repository
#Query(value = "SELECT :refId FROM product WHERE id = :id", nativeQuery=true)
public String findReferenceid(#Param(value = "id") long id, #Param(value = "refId") String refId);
Thanks.

SQL join table with a dynamic database name

I have a table which contains a employee ID and a database ID code in database DBX0 table 'FirstTable':
Employee ID
Database ID
123
1
456
2
789
2
149
3
The database name is stored in a different table 'databases' like :
Database ID
DB name
1
DBX1
2
DBX2
3
DBX3
So what I'm trying to do is join the first table with a separate table which is stored in the identified database, like
SELECT * from DBX0.dbo.FirstTable a join (SELECT [DB name] from DBX0.dbo.databases where [database ID] = a.[database ID]).dbo.OtherTable
(I don't think this will actually work with a subquery, but this would return the result that I want in theory.)
I have tried dynamic sql with a cursor to iterate through each database and then only return results based on database ID = database ID and Employee ID = Employee ID, but this returns the results in a separate result per database.
Also tried:
Select * from (SELECT * from DBX0.dbo.FirstTable a join '+#dbname+'.dbo.OtherTable on a.employeeid = b.employeeid and a.dbid = b.dbid)
in dynamic sql, but this didn't work either.
Is there any way to get all of the data in one result?

How can I order an IQueryable based on a field that is in another table?

I am a newbie to LINQ and c# and I have two tables :
Products
ProductStoreds
ProductStore has a foreignKey to Products with a field named ProductId(same for both tables).
I have an IQueryable named result from Products table. how can I sort it based on a field named status in ProductStore Table.
this is what I have tried and I have not been successful:
result = result.Where(p =>
p.ProductId == DbContext.ProductStores.Select(m => m.Product)
.OrderByDescending(m => m.Status).ToInt());
You can join and order by Product like this
var stores = DbContext.ProductStores;
result = from store in stores
join product in result on product.ProductId equals store.ProductId
orderby store.Status
select product;

Native query to Laravel eloquent

Good day, I have 3 tables which is products, users, and pivot product_user table.
and I want to fetch all the data in the main table if that row contains a pivot data.
Example
products table
ID Name
1 sample 1----------> is in pivot table **product_user**
2 sample 2----------> is in pivot table **product_user**
3 sample 3
users table
ID Name
1 user1-------------> is in the pivot table **product_user**
2 user2
3 user3-------------> is in the pivot table **product_user**
product_user
product_id user_id
1 1
3 2
In this scenario how can I fetch the data in the two main table that contains pivot data?
How can I achieve the result of this query in laravel way?
SELECT p.`name`, u.`first_name`
FROM product_user AS pu
LEFT JOIN products AS p
ON pu.`product_id` = p.id
LEFT JOIN `users` AS u
ON pu.`user_id` = u.`id`
result
name first_name
sample 1 user firstname
You have a BelongsToMany relationship Product ↔ User:
class ProductUser extends Model {
public function product() {
return $this->belongsTo(Product::class);
}
public function user() {
return $this->belongsTo(User::class);
}
}
$pivots = ProductUser::with('product', 'user')->get();
foreach($pivots as $pivot) {
// $pivot->user->first_name
// $pivot->product->name
}

Multiple WHERE clause for same Columns in TSQL

I am trying to query two tables that are in 1-to-many relationship.
What I've done is create a View knowing that i might end up with multiple records for the first table.
My scenario is as follows: I have a table "Items" and table "Properties".
"Properties" table contains an ItemsId column, PropertyId, PropertyValueId columns.
"Items" table/object contains a list of "Properties".
How would I query that "View" such that, I want to get all "Items" records that have a combination of "PropertyId" & "PropertyValueId" values.
In other words something similar to:
WHERE
(PropertyId = #val1 AND PropertyValueId = #val2) OR
(PropertyId = #val3 AND PropertyValueId = #val4) OR
(PropertyId = #val5 AND PropertyValueId = #val6)
WHERE clause is just a loop over "Items.Properties" collection.
"Items" represents a table of Items being stored in the database. Each & every Item has some dynamic properties, one or more. That's why I have another table called "Properties". Properties table contains columns:
ItemId, PropertyId, PropertyValue
"Item" object has a collection of Properties/Values. Prop1:val1, Prop2:val2, etc ...
Thanks
I may not have understood your requirement (despite the update) - if this or any other answer doesn't solve the problem please add some sample data for Items, Properties and the output and then hopefully it would become clear.
If Items is a specification of the property name-value pairs that you need (and has nothing to do with ItemId on Properties which seems strange...)
select p.itemid
from properties p
where exists (select 1 from items i where i.propertyId = p.propertyId and i.propertyValueId = p.propertyValueId)
group by p.itemid
having count(distinct p.propertyid) = (select count(*) from items)
This returns a set of itemids that have one (and only one) property value for each property defined in items. You can put the items count into a variable if you want.
I would use a query like this:
SELECT ItemId
FROM ItemView
WHERE (PropertyId = #val1 AND PropertyValueId = #val2)
OR (PropertyId = #val3 AND PropertyValueId = #val4)
OR (PropertyId = #val5 AND PropertyValueId = #val6)
GROUP BY ItemId
HAVING COUNT(*) = 3
The WHERE clause is the same as in your question, it only allows a row to be selected if the row has a matching property. You only need to make sure additionally that the items obtained have all the properties in the filter, which is done in the above query with the help of the HAVING clause: you are requesting items with 3 specific properties, therefore the number of properties per item in your result set (COUNT(*)) should be equal to 3.
In a more general case, when the number of properties queried may be arbitrary, you should probably consider passing the arguments in the form of a table and join the view to it:
…
FROM ItemView v
INNER JOIN RequestedProperties r ON v.PropertyId = r.Id
AND v.PropertyValueId = r.ValueId
GROUP BY v.ItemId
HAVING COUNT(*) = (SELECT COUNT(*) FROM RequestedProperties)