bind gridview using case statement in linq - divider

private void BindGrid()
{
var grid = (from a in dbcon.M_Linqs
join b in dbcon.M_Countries on a.Country equals b.CId
join c in dbcon.M_States on a.State equals c.SId orderby a.Id
select new
{
a.Id,
a.Name,
Country = b.CountryName,
State = c.StateName,
Gender = a.Gender == 1 ? "Male" : a.Gender == 2 ? "Female" : "-",
Hobby = a.Hobby = 1 ? "Cricket" : a.Hobby = 2 ? "Hockey" : a.Hobby=12?"CriCket,Hobby":"-"
}).ToList();
I want to bind gridview in which hobby column having integer value like 1 and 2 . if user enter 1 then it have to show Cricket if 2 then Hockey and ia 1,2 then Cricket,Hockey..
thanks in advance...

From a relational databases point of view you should have a table called Gender and a table called Hobby.
For example, Gender would have columns for ID and GenderName e.g. 1, Male and then you could simply join to this table and select the GenderName.

Related

Population of Dataset<Row> in java spark with reference from list of Maps

I have a Dataset<Row> df with columns (studentid,studentname,studentplace,student_strength)
Sample Output now :
studentid
studentname
studentplace
student_strength
1
Roberta
marketstreet
null
2
Oliver
lincolnStreet
null
3
Shayna
Eaton
null
4
Fechin
Vaughan
null
I have list of Map as below,
{1 = Student(1,11,Mathematics)},{2=Student(2,11,SocialScience)},{3=Student(3,11,Mathematics)},{4=Student(4,11,EnglishLiterature)}
key as studentid and values as Student object as below. for eg,
class Student
{
String studentid;
String student_age;
String stdudent_strength
}
I wanted to map student_strength column in each row in dataset df from taking value from the map using studentid column.
For eg, for each row using student id get the Student() from map and populate student strength in the row, No Join just iterating this way.
So after this the out put would be something like below,
studentid
studentname
studentplace
student_strength
1
Roberta
marketstreet
Mathematics
2
Oliver
lincolnStreet
SocialScience
3
Shayna
Eaton
Mathematics
4
Fechin
Vaughan
EnglishLiterature

How do I optimize MariaDB query with subqueries in FROM clause?

imagine these two tables.
Table A
ID col1 col2 col3
1 foo baz bar
2 ofo zba rba
3 oof abz abr
Table B
A_ID field_name field_value
1 first Jon
1 last Doe
2 first Adam
2 last Smith
etc..
Now I would like to have a query (current one looks like this)
SELECT
a.id,
a.col1,
a.col2,
(SELECT field_value FROM B WHERE A_ID = a.id AND field_name = 'first') as first_name,
(SELECT field_value FROM B WHERE A_ID = a.id AND field_name = 'last') as last_name
FROM A a
WHERE (SELECT COUNT(*) FROM B WHERE A_ID = a.id) = 2;
This query is working. What I would like to achieve would be something like this.
SELECT
a.id,
a.col1,
a.col2,
(SELECT field_value FROM b WHERE b.field_name = 'first') as first_name,
(SELECT field_value FROM b WHERE b.field_name = 'last') as last_name
FROM
A a,
(SELECT field_value, field_name FROM B WHERE A_ID = a.id) b
WHERE (SELECT COUNT(*) FROM b) = 2;
How would my approach look correctly? Is there any other way to get rid of the multiple queries of the table B?
Thank you!
I would replace your correlated subqueries with joins:
SELECT
a.id,
a.col1,
a.col2,
b1.field_value AS fv1,
b2.field_value AS fv2
FROM A a
LEFT JOIN B b1
ON a.id = b1.A_ID AND b1.field_name = 'first'
LEFT JOIN B b2
ON a.id = b2.A_ID AND b2.field_name = 'last';
This answer assumes that a left join from a given A record would only match at most one record in the B table, which, however, is a requirement anyway for your correlated subqueries to only return a single value.

Flexible search query using three tables

I have three tables Order,Customer,Address.
Edited :
I want to fetch all customer's who are registeretd with website.
If the customer has placed any order's i want to fetch the latest order from list of order's,
If customer has not placed any order then i want to put the field as blank as shown in below table
For eg :
Customer_Name Email ID Country Phone Latest_Order_Code
A xxxx xxxx x 1234
B yyyy yyyy y
C ffff tttt l 3456
D zzzz iiii o
Any help would be appreciated?
Refer to below query, where I have only fetch order code and Customer name. You can write more join and select fields based on your requirement.
select {o.code} as orderCode,
{c.name} as name,
{a.cellphone} as cellphone
from {order as o
join Customer as c on {c.pk} = {o.user}
join Address as a on {o.deliveryaddress} = {a.pk}
}
where {o.code} in ({{select max({code}) from {order} group by {user}}})
Update: Fetch all registered customers and their last order information
select t1.name, t2.orderCode, t2.cellphone
from
({{
select {pk} as userPk, {name} as name from {Customer}
}}) as t1
LEFT JOIN
({{
select
{o.code} as orderCode,
{o.user} as user,
{a.cellphone} as cellphone
from {order as o
join Address as a on {o.deliveryaddress} = {a.pk}
}
where {o.code} in ({{select max({code}) from {order} group by {user}}})
}}) as t2
on t2.user = t1.userPk
Detail post how to write complex flexible search query join using the dynamic tables?

How to write a correlated subquery for the below one?

Can anyone help me to write co-related subquery for the below non-correlated subquery?
SELECT e.emp_no,e.last_name FROM employees e,dept_emp d
WHERE e.emp_no = d.emp_no
AND d.dept_no = (SEELCT dept_no FROM dept_emp WHERE emp_no =
(SELECT emp_no FROM employees WHERE first_name = "Margareta" AND last_name = "Markovitch"));
This the answer for the above query :
Select E.Emp_No,E.Last_Name From Employees E Where Exists
( Select Dept_No From Dept_Emp D Where E.Emp_No=D.Emp_No)
And emp_no In (Select emp_no From Employees Where First_Name='Margareta' And Last_Name='Markovitch');

T-SQL, joining two tables between an integer column in the first table and a comma separated string column in the other

I have a situation like this (T-SQL):
Table 1: dbo.Printers
EmulationID EmulationDescription PrinterID Name
34,15,2 NULL 12 HP 1234
15,2 NULL 13 IBM 321
15 NULL 14 XYZ
Table 2: dbo.Emulations
EmulationID Description
34 HP
15 IBM
2 Dell
EmulationID column in dbo.Printers table is nvarchar/unicode string datatype, and integer datatype in the dbo.Emulations table.
Now I have to UPDATE the **EmulationDescription** column in the dbo.Printers table using a lookup on the dbo.Emulations table through the EmulationID column.
I need to get data like this in the dbo.Printers table:
EmulationID EmulationDescription PrinterID Name
34,15,2 HP,IBM,Dell 12 HP 1234
15,2 IBM,Dell 13 IBM 321
15 IBM 14 XYZ
Can someone help me in detail, on how to get this issue resolved ?
I created the user-defined function dbo.ParseIdListToTable to convert string data in one row into multiple rows. However, I do not know to proceed further, on how to exactly join and then update.
Any suggestion will be greatly appreciated.
You could do something like this:
CREATE FUNCTION [dbo].[CSVToTable] (#InStr VARCHAR(MAX))
RETURNS #TempTab TABLE
(id int not null)
AS
BEGIN
;-- Ensure input ends with comma
SET #InStr = REPLACE(#InStr + ',', ',,', ',')
DECLARE #SP INT
DECLARE #VALUE VARCHAR(1000)
WHILE PATINDEX('%,%', #INSTR ) <> 0
BEGIN
SELECT #SP = PATINDEX('%,%',#INSTR)
SELECT #VALUE = LEFT(#INSTR , #SP - 1)
SELECT #INSTR = STUFF(#INSTR, 1, #SP, '')
INSERT INTO #TempTab(id) VALUES (#VALUE)
END
RETURN
END
GO
DECLARE #Description VARCHAR(1000)
SELECT P.EmulationID,
(SELECT #Description = COALESCE(#Description + ',', '') + QUOTENAME(Description)
FROM dbo.Emulations
WHERE EmulationID IN (SELECT * FROM dbo.CSVToTable(P.EmulationID))) AS 'Emulation Description,
P.PrinterID,
P.Name
FROM dbo.Printers P

Resources