I have a linq query
from c in db.Custommer
join m in db.Membership on c.ID equals m.CustomerID
select (c.LastName + ", " + c.FirstName + " " + c.MiddleName);
The MiddleName could be NULL, how do I replace that null with a space or ignore it?
If I leave it this way, the query does not return any records for customers who don't have middle names.
You can do as such:
from c in db.Custommer
join m in db.Membership on c.ID equals m.CustomerID
select (c.LastName + ", " + c.FirstName + " " + (c.MiddleName ?? "");
This should do the trick :)
Related
I need the user id, user name, asesor nombre, asesor telefono, asesor correo and the tipo asesor but the json response repeat data.
this is the sql query in the spring reposotiry.
'''
#Query( value = "SELECT U.id as userid, u.name as cliente," +
"A.nombre, A.correo, A.telefono, TA.tipo as asesoria " +
"from tc_users U, tc_usuario_asesores UA , " +
"tc_asesor A,tc_tip_asesor TA " +
"where U.id = UA.userid " +
"and UA.asesorid= A.id " +
"and A.tipo_asesor = TA.id",
nativeQuery = true)
'''
and this query in sql workbench return this data
query in sql workbench
but the json response return this data
Json response
and dont know because this haappend, a need help with this, sorry for my bad inglish.
Your query returns a cartesian product because of the join.
You can add DISTINCT to get unique rows.
#Query( value = "SELECT DISTINCT U.id as userid, u.name as cliente," +
"A.nombre, A.correo, A.telefono, TA.tipo as asesoria " +
"from tc_users U, tc_usuario_asesores UA , " +
"tc_asesor A,tc_tip_asesor TA " +
"where U.id = UA.userid " +
"and UA.asesorid= A.id " +
"and A.tipo_asesor = TA.id",
nativeQuery = true)
I have this query:
#Query(value =
"select distinct t from TimesheetReport t "
+ "where t.month = :month and t.year = :year and t.approvedByMaster = false "
+ "and (:#{#rule.employee} is null or :#{#rule.employee.id} is null "
+ "or t.account.id = :#{#rule.employee.id}) "
+ "and t.id in "
+ "(select distinct dt.timesheetReport.id from DailyTime dt "
+ "where (:#{#rule.project} is null or :#{#rule.project.id} is null "
+ "or dt.project.id = :#{#rule.project.id}) "
+ "and (:#{#rule.client} is null or :#{#rule.client.id} is null "
+ "or dt.client.id = :#{#rule.client.id})) "
+ "or t.id in "
+ "(select distinct cdt.timesheetReport.id from CustomDailyTime cdt "
+ "where (:#{#rule.project} is null or :#{#rule.project.id} is null "
+ "or cdt.project.id = :#{#rule.project.id}) "
+ "and (:#{#rule.client} is null or :#{#rule.client.id} is null "
+ "or cdt.client.id = :#{#rule.client.id}) "
+ ")"
)
Set<TimesheetReport> findTimesheetsMatchingManualApprovalRule(LeaderManualApprovalRulesDTO rule, int month, int year);
I know this is caused by employee object being null.
This happens despite first checking if employee in rule is null and then checking if employee object id field is null. I still get "Property or field 'id' cannot be found on null".
Is it because SpEL needs to be able to evalute every parameter before firing a query?
#Repository
public interface GroupRepository extends JpaRepository<Group, String> {
//Other queries....
#Query(value = "with cte(group_id, parent_group_id, group_name) as( "
+ "select group_id, parent_group_id, group_name "
+ "from hea.hea_group "
+ "where group_id = ?1 "
+ "union all "
+ "select g.group_id, g.parent_group_id, g.group_name "
+ "from hea.hea_group g "
+ "inner join cte on cte.group_id = g.parent_group_id "
+ "where g.parent_group_id is not null "
+ ") select * from cte", nativeQuery = true)
List<Object> getChildGroups(String groupId);
}
Above is the query that I have written that should return the parent group and all of its children. The query does what it is suppose to do when I replace the ?1 with a hard coded group id value and change the method to have no parameters, but when I try to run it as above it returns nothing even though I'm passing in the exact same value that I was hard coding.
Below is the sql that is being generated by the query. When I replace the ? with a group id an run it on a test database it returns the results that it should.
with cte(group_id, parent_group_id, group_name) as( select
group_id,
parent_group_id,
group_name
from
hea.hea_group
where
group_id = ?
union
all select
g.group_id,
g.parent_group_id,
g.group_name
from
hea.hea_group g
inner join
cte
on cte.group_id = g.parent_group_id
where
g.parent_group_id is not null ) select
*
from
cte
The variables are zero based so ?0 is what you should use.
qry1 = "Select * from ser_complaint_master a,ser_complaint_status b,company_master c
where a.complaint_no=b.complaint_no
and a.allocation_code=c.co_code
and c.co_br_code='" + Session["BRCODE"] + "'
and a.Complaint_Date>='" + Frdat + "' and a.Complaint_Date<='" + Todat + "'
and a.status in ('Completed')
and a.complaint_type in('" + cmptype + "')";
How to use ORDER BY in select query if more than one tables are involved.
Add order by a.complaint_no to the end of the query.
I have two tables
EmpInf
EmpId,
EmpName,
Salary,
DepartNumber.
Dept
DeptNo,
Deptname,
I also have a listview1 and dropdownlist1 which is bound to EmpInf.EmpName
While passing a particular query
FilterControl.DataClasses1DataContext obj = new DataClasses1DataContext();
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
var a = from r in obj.EmpInfs join s in obj.Dept1s on r.DeptNumber equals s.DeptNo where r.EmpName == "'" + DropDownList1.SelectedValue + "'" select s;
ListView1.DataSource = a;
ListView1.DataBind();
}
Whenever I select a particular name from a dropdownlist, it returns No data was returned. What particular code am I missing or is there any other error?
Put a breakpoint in SelectedIndexChanged1 and look at the value of SelectedValue to make sure DropDownList1.SelectedValue has the Employee name. You can also try DropDownList1.SelectedText.
string selected = DropDownList1.SelectedValue.ToString();
// e = employee | d = department
var query =
from e in obj.EmpInfs
join d in obj.Dept1s on e.DeptNumber equals d.DeptNo
where e.EmpName == "'" + DropDownList1.SelectedValue + "'"
select d;
Change this line:
where e.EmpName == "'" + DropDownList1.SelectedValue + "'"
to this one:
where e.EmpName == selected
OK, my last attempt here... Do this before databinding:
ListView1.DataSource = query.ToList();
Most likely the problem here is with your addition of the single quotes on the search criteria in your Where clause:
var a = from r in obj.EmpInfs
join s in obj.Dept1s on r.DeptNumber equals s.DeptNo
where r.EmpName == "'" + DropDownList1.SelectedValue + "'"
select s;
Assuming DropDownList1.SelectedValue is "Smith" then your generated sql will be along the lines of:
SELECT *
FROM <tables>
WHERE EmpName = ''Smith''
Notice the double single quotes. To double check this, put a breakpoint after your query is generated and then call .ToString() on it to get the equivalent TSQL. To fix this, remove the "'" from your LINQ query as that will be automatically added for string parameters:
var a = from r in obj.EmpInfs
join s in obj.Dept1s on r.DeptNumber equals s.DeptNo
where r.EmpName == DropDownList1.SelectedValue
select s;