Unable to disting the record in Link Expression - asp.net

I have two tables: TblAppointmentTypes and TblEmployeeInfo. There is many to many relation between these two tables with a joined table that is TblEmployeeServices. There are two records in TblAppointmentTypes and four records in TblEmployeeInfo. All the two records of TblAppointmentTypes is assigned to all of the records in TblEmployeeInfo, that there is eight records in TblEmployeeServices. I want to retrive all the services that are assigned to all the four Employees, it should return 2 that is assigned to all the four employees, but my query return 8 records, four duplicate services.
I am using Telerik Open Access ORM. Here is the code:
public static List<TblAppointmentType> GetAllAppointmentType(List<int> employeeIDs)
{
var list = new List<TblAppointmentType>();
if (employeeIDs != null && employeeIDs.Count > 0)
{
var dc = new Entities();
list = (from a in dc.TblAppointmentTypes.Distinct()
join e in dc.TblEmployeeServices on a.ID equals e.AppointmentTypeID
where a.IsDeleted == false && employeeIDs.Contains<int>(e.EmployeeID)
select a).ToList();
}
return list;
}

As I figured it out, You want to get all AppointmentTypes which are related to all Employees.
It means that AppTypes that are related to 3 Employees (in your case) should be excluded.
So, the most efficient code that I have tried is like this :
list = (from a in dc.TblAppointmentTypes
join e in dc.TblEmployeeServices on a.ID equals e.AppointmentTypeID
where a.IsDeleted == false
group e.EmployeeID by a into g
where g.count() == employeeIDs.Count()
select g.Key).ToList();

Related

Count of group by statement AX2012 R3

I want to count group by statement values and write control.
There is a table which have 3 fields; ItemId, TaxGroup and TaxItemGroup. Same itemId must have same taxgroup and taxItemGroup values. If lines which have same ItemId have different tax values, i should throw an exception.
I wrote this code but this return number of all records. How can i write this control?
while select count(RecId) from ActivityLineExtraLcl
group by Itemid, TaxGroup, TaxItemGroup
where ActivityLineExtraLcl.Amount != 0
if(ActivityLineExtraLcl.RecId > 1 )
throw error("Same ItemIds can't have different values!");
The logic you're attempting won't work as written and it doesn't really make sense. You can try something like the below. This identifies the actual items. Your code (if it worked) just looks to see if the entire table ActivityLineExtraLcl has any issues.
ActivityLineExtraLcl ActivityLineExtraLcl;
ActivityLineExtraLcl ActivityLineExtraLclExists;
while select ItemId from ActivityLineExtraLcl
group by ItemId
where ActivityLineExtraLcl.Amount != 0
exists join ActivityLineExtraLclExists
where ActivityLineExtraLclExists.ItemId == ActivityLineExtraLcl.ItemId &&
ActivityLineExtraLclExists.Amount != 0 &&
(ActivityLineExtraLclExists.TaxGroup != ActivityLineExtraLcl.TaxGroup ||
ActivityLineExtraLclExists.TaxItemGroup != ActivityLineExtraLcl.TaxItemGroup)
{
error(strFmt("Item '%1' has different tax values", ActivityLineExtraLcl.ItemId));
}

ASP.NET MVC 5 Join on Between, no primary key

I have some interesting code to look at.
I have three tables:
Table A has 4 columns:
TablePK
UserID
TableBFKID
Score
Table B has 3 columns:
TablePK
Name
ShortName
Table c has 4 columns:
TablePK
ScoreMin
ScoreMax
Modifier
So when the full join happens it looks like this:
SELECT B.ShortName
, A.Score
, C.Modifier
FROM TableA A
INNER JOIN TableB B ON a.TablePK= B.TablePK
INNER JOIN TableC C ON A.Score BETWEEN C.ScoreMin AND C.ScoreMax
The results would look like this:
ShortName, Score, Modifier. EX:
CHA, 19, 4
Now I know how to do an Entity Framework join if there is an actual PK or FK, or even if there is only a 0:1 relationship.
But how do you do the join when there is neither a PK nor an FK?
LINQ, including LINQ to Entities, only supports equi-joins.
But you can run SQL directly:
var res = myContext.Database.SqlQuery<TResult>("SQL goes here", parmeters...);
and EF will map the columns of the result set to the properties of TResult (which needs to other connection to the context: it does not need to be an entity type with a DbSet typed property in the context).
In this case I wouldn't try to join them, just use a sub-select in your linq to select from the un-joined table where the scores are between your wanted ranges.
var results = context.TableA
.Select(a => new {
score = a.Score, //add all needed columns
tableCs = context.TableC.Where(c => a.Score >= c.ScoreMin && a.Score <= c.ScoreMax)
});

How to use if statement when using linq to sql join?

I'm using linq to sql on my asp.net project and i locked in somewhere. I select data amoung relationship tables and I use following codes. But the question is if supplierID is different than 0 there is no problem for query. Query is working as successful. But if supplierId is 0 query returns nothing. I want to use if statement when i join tables but how ?
My Codes,
EnvanterDataContext dc = new EnvanterDataContext();
var invent = from equipment in dc.tEquipments
where equipment.EquipmentID == id
orderby equipment.EquipmentID descending
join supplier in dc.tSuppliers on equipment.SupplierID equals supplier.SupplierID
join model in dc.tModels on equipment.ModelID equals model.ModelID
join equipmentType in dc.tEquipmentTypes on equipment.TypeID equals equipmentType.EquipmentTypeID
join equipmentStatus in dc.tEquipmentStatus on equipment.StatusID equals equipmentStatus.EquipmentStatusID
select equipment;
I want to use
EnvanterDataContext dc = new EnvanterDataContext();
var invent = from equipment in dc.tEquipments
where equipment.EquipmentID == id
orderby equipment.EquipmentID descending
if(equipment.SupplierID != 0)
join supplier in dc.tSuppliers on equipment.SupplierID equals supplier.SupplierID
join model in dc.tModels on equipment.ModelID equals model.ModelID
join equipmentType in dc.tEquipmentTypes on equipment.TypeID equals equipmentType.EquipmentTypeID
join equipmentStatus in dc.tEquipmentStatus on equipment.StatusID equals equipmentStatus.EquipmentStatusID
select equipment;
Thanks inadvance.

Syntax for collecting data in var query

I am collecting some data from database using var query and storing it in an integer. Below is the code:
var query1 = from meet_emp in db.Meet_Emps
where meet_emp.Employee_ID == empid
select meet_emp.Meeting_ID;
int meetNum = query1.First();
Here query1 contains multiple meeting ids. I wish to store all id's into an int variable or int array as I would be using each meeting id later into another query. With syntax "int meetNum = query1.First()", I get only the first meeting id.
How do I get all the meeting id's
You can leave the data just as it is in variable query1, then join it later with your final query that uses that list:
from blah in db.Stuff
join query1 on query1.Meeting_ID equals blah.Meeting_ID
select ...;
Just change to ToList then you will have a list of IDs
var query1 = from meet_emp in db.Meet_Emps
where meet_emp.Employee_ID == empid
select meet_emp.Meeting_ID;
var meetNum = query1.ToList();

Querying on count value having ()

I'm having difficulty with a query which displays records according to their fill rate.
For instance, a vacancy can have no bookings or some bookings. If a vacancy has bookings, they can be in the form of 'active [1]', 'pending [0]'. The query I have written so far works if the vacancy has booking records but I can't get it to work if it doesn't have booking records.
My query (which works) for vacancies with a booking is as follows:-
SELECT v.*, j.job_category_name, bu.business_unit_name
FROM vacancy v
INNER JOIN job_category j ON j.job_category_id = v.job_category_id
INNER JOIN business_unit bu ON bu.business_unit_id = v.business_unit_id
INNER JOIN booking b ON b.vacancy_id = v.vacancy_id
INNER JOIN booking_status bs ON bs.id = b.booking_status_id
WHERE
v.vacancy_status <> 'revoked' AND
v.vacancy_reference <> 'auto-generated booking' AND
v.business_unit_id IN (series of primary keys) AND
(bs.booking_status_type_id = 1 OR bs.booking_status_type_id = 2)
GROUP BY v.vacancy_id
HAVING v.vacancy_limit > count(b.booking_id)
ORDER BY v.vacancy_id DESC
I thought by changing the join of b and bs to LEFT JOIN would have worked, but it hasn't.
Any ideas?
Without a copy of your schema to work from, it's difficult to tell exactly, but when you changed booking and bookingstatus to LEFT JOINs, did you also modify your WHERE clause so that it read something like:
WHERE
v.vacancy_status <> 'revoked' AND
v.vacancy_reference <> 'auto-generated booking' AND
v.business_unit_id IN (series of primary keys) AND
(ISNULL(bs.booking_status_type_id, 1) = 1 OR ISNULL(bs.booking_status_type_id, 2) = 2)
i.e. Ensured that the full WHERE clause would be satisfied, thus not stripping out the records where all the values for columns from booking and bookingstatus were NULL?
Try LEFT OUTER JOIN for the tables for which the joins may return 0 matches.
For eg:- in your case have LEFT OUTER JOIN for b and bs and check

Resources