I am populating a combobox in C# & SQL.I have two tables to get data from them.
Table1
Id CategoryName
1 Salary
2 Utility
3 Other
Table2
Id SubCategoryName CatId
1 XYZ Salary 1
2 YY Salary 1
3 Car Exp 3
Now i want to show them all in a dropdown so that User select any of them and then i will save its Id.
How i will Know that selected Id is from Table1 OR Table2 ?
You will have to play around with fake id's. For example, you could create a new class with a new Id and map that new id to the tables manually. For example:
Class DisplayItem
Id 1 Name Salary
Id 2 Utility
Id 3 Other
Id 4 XYZ Salary
ID 5 Y Salary
Id 6 Car Exp
Then you will need code that looks like this
public int GetTableId(int DisplayItemId)
{
if (DisplayItemId < 4)
return 1;
return 2;
}
Related
I have a question regarding Linq in C#.
Let's say that I have 3 tables "Company", "Employee" and "Job".
Company
--------
Id - int (PK)
Name - string
Employee
--------
Id - int (PK)
CompanyId - int (FK on Company.Id)
Name - string
Job
--------
Id - int (PK)
CompanyId - int (FK on Company.Id)
EmployeeId - int (FK on Employee.Id)
Name - string
Something like that:
enter image description here
The important thing is that every work must be connected to a company but not necessarily to an employee. However, each employee must be connected to a company. For example we can have racord like that:
Company
--------
Id Name
1 'A'
2 'B'
3 'C'
Employee
--------
Id CompanyId Name
1 1 'A'
2 1 'B'
3 2 'C'
Job
--------
Id CompanyId EmployeeId Name
1 1 1 'clean'
2 1 2 'wash'
3 2 2 'buy'
4 3 NULL 'sell'
And now with linq I would like to get all jobs that are assigned to this employee and other employees from the same company.
So in this case it will should are jobs with id 1 and 2 becouse employee 1 is assigned to company 1 and job with id 2 also has assigned to this company. How could I achieve this using linq but something like this:
_context.Job.Where (x => x)
It is important to make only one query to the database.
Tkanks.
You can simplify the problem by thinking of it in two steps: find the company for a given employee, find all the jobs for that company:
var companyToFind = from e in Employee
where e.Id == empToFind
select e.CompanyId;
var jobsToFind = from j in Job
where companyToFind.Any(cid => cid == j.CompanyId)
select j;
In LINQ to SQL, EF Core 2.2 and EF Core 3.x the Any is translated to an EXISTS query in SQL.
Note since you know there should be only one answer to companyToFind, sending two queries to the database may be more efficient.
For LINQ to SQL or EF Core 3.x, you could also nest the first query and reduce to a single result:
var jobsToFind = from j in Job
where (from e in Employee
where e.Id == empToFind
select e.CompanyId).First() == j.CompanyId
select j;
i want to fetch all the records having max product id from product table. the records in the product table are stored as category wise like following
Product id Category id Product name Product image
1 1 product 1 path
2 1 Product 2 path
1 2 Product 1 path
2 2 Product 2 path
3 2 Product 3 path
1 3 Product 1 path
and i have Category table like following
Category id Category name
1 Category 1
2 Category 2
3 Category 3
so i need to write query that return all records of max product in each category (1,2,3 here in this example) so we'll get the result like
# Product id Category id Product name Product image
1. 2 1 product 2 path
2. 3 2 product 2 path
3. 1 3 product 1 path
note:- product id is primary key and generated according to it's category
category id is foreign key in product table.
i am using this query/database in my C# application. I am using MS-Access.
Ok this will help:
"SELECT * From Product WHERE Categoryid=(SELECT Max(Count(*)) from product group by Categoryid)"
this should help
SQL Server -
select p.* from
(
select max(productid) productid,categoryid from products group by categoryid
) a join products p on p.productid = a.productid
and p.categoryid = a.categoryid
ACCESS -
select [p].* from
(
select max(productid) as pid,categoryid from products group by categoryid
) as [a] inner join [products] as [p] on [p].[productid] = [a].pid
and [p].categoryid = [a].categoryid
I have 3 tables:
Table 1
CustomerID Name Sname Zipcode
1 test1 test1 xxx2
2 test2 test2 yyy2
3 test3 test3 xxx2
Table 2
StaffID Name sname
1 Jack Mack
2 Jhon Addison
Table 3
ID CustomerID StaffID
1 1 1
I am not sure if SQL can do this, So in C# what I want is if StaffID 2 is not in Table 3 and if a staff wants to visit all the customers in zipcode xx2 then by click on a visit button he should be inserted for all the zipcode. So if I select Zipcode xxx2 then the query should first check if I am not visiting same Zipcode for a specific, if not then add the person into table 3 so the updated table 3 will look something like this:
ID CustomerID StaffID
1 1 1
2 1 2
3 3 2
As you can see, StaffID 2 would like to visit xxx2 zipcode as a result he gets 2 entries into Table 3 because there are two CustomerID with Zipcode same as xxx2.
I have did some online research I would this IF EXISTS (SELECT but it just add one entry. Also I guess I will be expecting 3 parameter from C# zipcode, CustomerID and StaffID
From your description it appears you just need a simple insert statement.
INSERT INTO Table3 (CustomerID, StaffID)
select CustomerID, #StaffId from Table1
where Zipcode = #zipcode
and NOT EXISTS(select StaffID from Table3 where StaffID = #staffId)
Try this sql query, assuming you are passing #StaffId, #CustomerId and #Zip as parameter
INSERT INTO Table3 (CustomerID, StaffID)
SELECT CustomerID, #StaffId FROM Table1
WHERE Zipcode = #zip
AND NOT EXISTS(SELECT StaffID FROM Table3 WHERE StaffID = #staffId
AND CustomerID = #CustomerId)
I have a table which is similar to Key-Value pair design structure. I would like to query the table based on the key and value, but I am facing some problem. Below is my table structure and expected output.
Table 1:
ID KEY VALUE
1 NAME abc
2 AGE 12
3 DEPARTMENT CCB
4 NAME xyz
5 AGE 13
6 DEPARTMENT TSS
7 NAME cde
8 AGE 12
9 DEPARTMENT TMS
Table 2:
KeyId KeyName
1 Name
2 Department
3 Age
for easy understanding I entered directly the key names in Table 1, actually there exists a foreign key relation between table 1 and table 2.
Also table 2 can hold anything as KeyName not necessarily Name, Department and Age.
Expected output is : I need to get the data with the Name contains %c% and Age = 12.
Name Age Department
abc 12 CCB
cde 12 TMS
NOTE: I am looking for a EF solution i.e., linq query, not sql query.
Options I tried out, below is the query:
var temp = (from c in table1
where c.Name.Contains("C") || C.Age == 12
Select new { C.Name, C.Age })
Thought of applying "OR" condition so that I get both the condition satisfied and then filter using "AND" in the in-memory once I fetch the data from database. But I was not able to succeed.
Try this:
var result = input.Select((x,i)=>new {x,i})
.GroupBy(a=>a.i/3)
.Where(g=>g.FirstOrDefault().x.Value.Contains("c") &&
Convert.ToInt32(g.Skip(1).FirstOrDefault().x.Value) == 12)
.Select(g=> new {
Name = g.FirstOrDefault().x.Value,
Age = Convert.ToInt32(g.Skip(1).FirstOrDefault().x.Value),
Department = g.Skip(2).FirstOrDefault().x.Value
});
Not sure if it works in EF but looks like if it doesn't, you have to execute the query in client side with AsEnumerable() before appending any method after input.
I have successfully simulated an Oracle CONNECT BY statement in SQL Server 2008 by following these 2 previous answers here and here and adjusting to get the results I need. But how do I do this in LINQ?
Here is an example of what I am doing using a dummy database:
CREATE TABLE Employee(
EmployeeID INT IDENTITY(1,1) PRIMARY KEY,
Department INT NOT NULL,
EmployeeName VARCHAR(40) NOT NULL,
PeckingOrder INT NOT NULL,
HigherDepartment INT NULL)
INSERT INTO Employee (Department,EmployeeName,PeckingOrder,HigherDepartment)
VALUES (1,'Bart',1,NULL),(2,'Homer',1,1),(2,'Marge',2,NULL),
(3,'Lisa',1,2),(3,'Maggie',2,2),(3,'Santas Helper',3,1)
EmployeeID Department EmployeeName PeckingOrder HigherDepartment
1 1 Bart 1 NULL
2 2 Homer 1 1
3 2 Marge 2 NULL
4 3 Lisa 1 2
5 3 Maggie 2 2
6 3 Santas Helper 3 1
and this is the SQL used to return the heirachy:
WITH n(level, PeckingOrder, Department, EmployeeName, HigherDepartment) AS
(SELECT 1, PeckingOrder, Department, EmployeeName, HigherDepartment
FROM Test.dbo.Employee
WHERE Department = 3
UNION ALL
SELECT n.level + 1, nplus1.PeckingOrder, nplus1.Department, nplus1.EmployeeName, nplus1.HigherDepartment
FROM Test.dbo.Employee as nplus1
JOIN n ON n.HigherDepartment = nplus1.Department)
SELECT MAX(level) AS level, PeckingOrder, Department, EmployeeName, HigherDepartment
FROM n
GROUP BY PeckingOrder, Department, EmployeeName, HigherDepartment
ORDER BY MAX(level) DESC, PeckingOrder ASC
level PeckingOrder Department EmployeeName HigherDepartment
3 1 1 Bart NULL
2 1 2 Homer 1
2 2 2 Marge NULL
1 1 3 Lisa 2
1 2 3 Maggie 2
1 3 3 Santas Helper 1
You could use ExecuteQuery:
class YourRow
{
public int level {get; set;}
public int PeckingOrder {get; set;}
...
}
using (var db = new LinqDataContext())
{
var list = db.ExecuteQuery<YourRow>(
#"
WITH n(level, PeckingOrder, Department, EmployeeName, HigherDepartment) AS
(SELECT 1, PeckingOrder, Department, EmployeeName, HigherDepartment
...
";
}
Or perhaps better, create a view that contains the query, and use LINQ to read from the view.