select *
from [InterViewerComment]
where commentID in (select max(commentID) as commentID
from [InterViewerComment]
where jobID = 45
group by qenID)
This query is correct in SQL, but I want to rewrite it in Entity Framework.
Basically, I want the latest comment for each qenID based on job ID.
Other way to do the same
var query = " select qendidateList.qenName,InterViewerComment.*, candidate_status.status, 0 as ExamMarks, 0 as Skills from [InterViewerComment] " +
" left outer join qendidateList on InterViewerComment.qenID = qendidateList.qenID " +
" left outer join candidate_status on InterViewerComment.candidate_status = candidate_status.Candidate_status " +
" where commentID in( select max(commentID) as commentID from[InterViewerComment] where jobID = 45 group by qenID)";
var CandidateComm_ = db.Database.SqlQuery<interViewerComment>(query).ToList();
Related
Actually i have two tables i want to display records of the use of second table. Please help me in this. Let me provide you example.
[TABLE1]
LocationID LoginID Location_Name
1 101 A
2 102 B
3 103 C
[TABLE2]
ID LoginID No_Of_Item Location ToLocation
1 101 5 1 2
2 102 6 2 3
3 103 7 1 3
This is my database. Now i want to show [TABLE2] records with location name. But i am unable to do that. Please help me in this LINQ Query.
This is my code.
public IQueryable<StockTransferViewModel> GetAllStockTransferDetailByLoginId(string LoginId)
{
var StockList = (from aspuser in context.AspNetUsers
join cus in context.Customers on aspuser.Id equals cus.LoginID
join transfer in context.StockTransfers on aspuser.Id equals transfer.LoginID
where transfer.LoginID == LoginId
select new StockTransferViewModel
{
ID = transfer.ID,
LoginID = transfer.LoginID,
Date_Of_Transfer = transfer.Date_Of_Transfer,
No_Of_Sku = transfer.No_Of_SKU,
FromLocationName=transfer.Location,
ToLocation=transfer.ToLocation,
}).AsQueryable();
return StockList;
}
If TABLE1 is named Locations, you'll likey want to add the following lines after the line starting with join transfer in:
join fromLocation in context.Locations on transfer.Location equals fromLocation.LocationID
join toLocation in context.Locations on transfer.Location equals toLocation.LocationID
Then in your select statement, you'll want:
FromLocationName = fromLocation.Location_Name,
ToLocation = toLocation.Location_Name,
I am trying to convert below query in entity framework core but not able to access field in select query after apply group by.
Select ProdCatg.Category As [Category]
,Prod.FKProdCatgID As [FKProdCatgID]
,SUM(Dtl.Qty + LD.ProdConv1 + Trn.CashAmt ) As [DistributionRateValue]
From tblSalesInv_Dtl Dtl
Left Join tblSalesInv_Trn Trn on Trn.PKID=Dtl.FKID And Trn.FKSeriesID=Dtl.FKSeriesID
Left Join tblProdLot_Dtl LD on Dtl.FKLotID=LD.PKLotID And LD.FKProdID=Dtl.FKProdID
Left Join tblProd_Mas Prod ON PKProdID=Dtl.FKProdID
Left Join tblProdCatg_Mas ProdCatg On PKProdCatgID=Prod.FKProdCatgID
Where Trn.DraftMode=0
Group By Prod.FKProdCatgID,ProdCatg.Category
Order By Category
I am trying convert like this but but not able to access Dtl.Qty + LD.ProdConv1 + Trn.CashAmtfields
var result1 = (from dtl in _context.TblSalesInvDtl
join ser in lstSeries on dtl.FkseriesId equals ser
join trn in _context.TblSalesInvTrn on new { s1 = dtl.Fkid, s2 = ser } equals new { s1 = trn.Pkid, s2 = trn.FkseriesId }
join lot in _context.TblProdLotDtl on new { s1 = dtl.FklotId, s2 = dtl.FkprodId } equals new { s1 = lot.PklotId, s2 = lot.FkprodId }
join p in _context.TblProdMas on dtl.FkprodId equals p.PkprodId into pr
from Prod in pr.DefaultIfEmpty()
join pl in _context.TblProdMas on dtl.FklinkedProdId equals pl.PkprodId into plid
from ProdLinked in plid.DefaultIfEmpty()
join t in _context.TblTaxMas on dtl.FktaxId equals t.PktaxId into tid
from Tax in tid.DefaultIfEmpty()
join c in _context.TblProdCatgMas on Prod.FkprodCatgId equals c.PkprodCatgId into cid
from Catg in cid.DefaultIfEmpty()
where trn.EntryDate == Convert.ToDateTime("1-12-2018")
group Catg by new { Prod.FkprodCatgId, Catg.Category } into gb
//, dtl, lot, Prod
select new
{
CategoryName = gb.FirstOrDefault().Category,
DistributionRateValue = gb.Sum(a => (gb.Key.dtl.Qty + gb.Key.lot.ProdConv1+ gb.Key.lot.CashAmt)),
FKProdCatgID= gb.Key.Prod.FkprodCatgId,
}
).ToList();
can anybody help me how can I access multiple table fields in sum with groupby in entity framework core.
I am looking for a query where i can return all work items and their relation from a area path.
for example : project 1
i need all Featured all Userstories mapped to it all workitem and Bug mapped to userstories,
in short if i took a bug from the object i need somthing like parent id where i can match with the userstory.
string query1 = " SELECT * FROM WorkItemLinks " +
" WHERE ( [System.IterationPath] Under 'iteration1' )" +
" AND ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward' )" +
" ORDER BY [Microsoft.VSTS.Scheduling.StartDate]";
which throwing an error like below
An exception of type 'Microsoft.TeamFoundation.WorkItemTracking.Client.ValidationException' occurred in Microsoft.TeamFoundation.WorkItemTracking.Client.dll but was not handled in user code
Additional information: TF51005: The query references a field that does not exist. The error is caused by «[System.IterationPath]».
when i checked the dll's are refereed correctly and when i re wrote the query like this the query is working fine
string query = " SELECT * FROM WorkItems"+
" WHERE ( [System.IterationPath] Under 'iteration1' )" +
//" AND ([System.State] = 'Active' OR [System.State] = 'Assessed' ) "+
//" AND ( [Microsoft.VSTS.Scheduling.StartDate] <= '09/13/2017' AND [Microsoft.VSTS.Scheduling.FinishDate] >= '09/13/2017' )"+
" ORDER BY [Microsoft.VSTS.Scheduling.StartDate]";
but this query result does not give the relation ship that if a work item mapped as a child to other i need parent id in the work item object. how to get that. Thanks in advance.
You can try below query:
Install Nuget Package Microsoft.TeamFoundationServer.ExtendedClient for the project.
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System;
namespace _0925_WIQL
{
class Program
{
static void Main(string[] args)
{
TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(
new Uri("http://server:8080/tfs/CollectionLC"));
WorkItemStore workItemStore = (WorkItemStore)tpc.GetService(typeof(WorkItemStore));
string query1= " SELECT * FROM WorkItemLinks " +
" WHERE ( Source.[System.IterationPath] Under 'TeamProject\\Iteration 1' )" +
" AND ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward' )" +
" ORDER BY [Microsoft.VSTS.Scheduling.StartDate]";
Query query = new Query(workItemStore, query1);
WorkItemLinkInfo[] witLinkInfos = query.RunLinkQuery();
foreach (WorkItemLinkInfo witinfo in witLinkInfos)
{
.......
}
Besides, you can also use Wiql Editor. If you want to get all the parent workitems (IDs) from a specific child work item (ID), you can use below WIQL:
SELECT
[System.Id],
[System.WorkItemType],
[System.Title],
[System.AssignedTo],
[System.State]
FROM workitemLinks
WHERE ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward')
AND ([Target].[System.Id] = 25)
ORDER BY [System.Id]
MODE (Recursive, ReturnMatchingChildren)
I am trying to get an employee manager's first and last name IF the employee has a manager ( Some employee's do not ex CEO etc ). Currently it returns the employee name for manager and if there is no ManagerID in contact it wont return any values
Here is my general table structure for the tables I'm trying to access:
Employee
EmpID
EmployeeNumber
StartDate
isManager
Status ( full time / part time )
ContactID
Contact
ContactID
Fname
Lname
ManagerID
Department
DeptID
Name
DeptHistory
DeptHistID
DeptID
EmpID
PosTitle
StartDate
EndDate
ModifiedDate
And here is the query I have been manipulating:
SELECT
dh.StartDate, dh.PositionTitle, d.Name,
e.EmployeeNumber, e.Classification, e.Status,
c1.FirstName, c1.LastName, c1.SIN,
c1.DateOfBirth, c1.PhoneNumber, c1.EmailAddress, c1.AddressLine1,
c1.AddressLine2, c1.PostalCode, c1.City, c1.Province,
(c2.FirstName+ ' ' + c2.LastName) AS Manager
FROM
Person.Contact c1
JOIN
HumanResources.Employee e ON c1.ContactID = e.ContactID
JOIN
HumanResources.EmployeeDepartmentHistory dh ON e.EmpID = dh.EmpID
JOIN
HumanResources.Department d ON dh.DepartmentID = d.DepartmentID
JOIN
Person.Contact c2 ON c2.ManagerID = e.EmpID
WHERE
e.EmpID = #empID
Use LEFT OUTER JOIN instead of JOIN, so the query would look like this:
LEFT OUTER JOIN Person.Contact c2 ON c2.ManagerID = e.EmpID
OUTER word is optional so you can use just LEFT JOIN.
See Using Left Outer Joins on Microsoft's TechNet.
Previously in SQL I would use something like this to rank my search results:
--search product name
SELECT tblProduct.ProductID, 20 AS Ranking
FROM tblProduct
INNER JOIN tblManufacturer ON tblProduct.ManufacturerID=tblManufacturer.ManufacturerID
LEFT OUTER JOIN tblProductCollection ON tblProduct.CollectionID=tblProductCollection.CollectionID
WHERE tblManufacturer.Name + ISNULL(' ' + tblProductCollection.CollectionName, '') + ' ' + tblProduct.Name LIKE '%' + #term + '%' AND tblProduct.Active = 1
UNION ALL
--search product exact name
SELECT tblProduct.ProductID, 200 AS Ranking
FROM tblProduct WHERE Name = '%' + term AND tblProduct.Active = 1
UNION ALL
This example says if your search term is contained in the name: 20 is the rank, if you match the name exactly: 200 is the rank. Union the tables together, order by Ranking(descending) and hey presto!
I'm trying to do this in LINQ this time round and am unsure how to go about doing it, to be honest I'm unsure my previous example was the best way to do it originally.
So, I have a Product Entity mapped to my database and I've added a property in my partial class called SearchRanking:
var query = from p in db.Products
where p.Name.Contains(term)
select p;
var query2 = from p in db.Products
where p.Name.ToLower() == term
select p;
Somehow I need to set the properties like so:
var query = from p in db.Products
where p.Name.Contains(term)
select p, p.SearchRanking = 20;
var query2 = from p in db.Products
where p.Name.ToLower() == term
select p, p.SearchRanking = 200;
Am I on the right track?
If you're wanting to create a new anonymous type you could do this:
var foundProducts = (from p in products
where p.Name.Contains(term)
select new Product
{
ProductId = p.ProductId,
Category = p.Category,
Brand = p.Brand,
SearchRanking = p.Name.ToLower() == term ? 200 : 20
}).OrderBy(s => s.SearchRanking).Take(20);
I would do something like this;
var query = (from p in db.Products
where p.Name.Contains(term)
select p).ToList().ForEach(p => p.SearchRanking = 20);
A more efficient way would be to;
var query = (from p in db.Products
where p.Name.Contains(term)
select new Product
{
Id = p.Id,
//set the other props here
SearchRanking = 20
}).ToList();