I would like to write following query to an NHibernate-QUery in order to get a list of PRESC_DEL ordered by PRESC.ADMINDATE
select * from PRESC_DEL, PRESC
where PRESC.ID = PRESC_DEL.ID_PRESC
and PRESC_DEL = PRESC_DEL.ID_DISTRIBUTIE
or by PRESC.ADMINDATE
How do I make the join?
var query = (from pd in session.Query<PrescDel>()
where pd.??? = pd.IdDistribute
orderby pd.AdminDate
select pd);
var results = query.Fetch(x => x.Presc).ToList();
Related
I have a query wherein I need to get the count from 2 different tables. Here is a very simple form of the query (my query has more joins and conditions but this is the part I am stuck on):
select (select count(1) from Table1) as One, (select count(1) from Table2) as Two
The following linq queries works but I would like to do the above with a single linq query sent to the SQL Server. This query and many other versions I have tried, result in 2 queries sent to the server:
var query1 = from m in this.Table1 select m;
var query2 = from sr in this.Table2 select sr;
var final = new { One = query1.Count(), Two = query2.Count() };
I also tried this and this also sends 2 queries:
var final = from dummy in new List<int> { 1 }
join one in query1 on 1 equals 1 into ones
join two in query2 on 1 equals 1 into twos
select new { One = ones.Count(), Two = twos.Count()};
You need to make it a single LINQ query that can be translated:
var final = (from m in this.Table1.DefaultIfEmpty()
select new {
One = (from m in this.Table1 select m).Count(),
Two = (from sr in this.Table2 select sr).Count()
}).First();
Note that putting the sub-queries into an IQueryable variable will cause three separate queries to be sent.
Alternatively, since Count() doesn't have a query syntax equivalent, this is a little more compact in lambda syntax:
var final = this.Table1.DefaultIfEmpty().Select(t => new {
One = this.Table1.Count(),
Two = this.Table2.Count()
}).First();
How can I construct the below sql query in linq query to get the results ?
SELECT PageNumber from LMS_SurveyQuestion WHERE SurveyQuestionID IN
(SELECT SurveyQuestionID from LMS_SurveyQuestionOptionChoice
WHERE NextPageNumber = 4) and SurveyID = 1
Have a look at this article. Basically, if you want to implement SQL IN query in LINQ, you need to construct an inner query first, and then use the Contains() method. Here's my attempt:
var innerQuery = (from log in LMS_SurveyQuestionOptionChoice where log.NextPageNumber = 4 select log.SurveyQuestionID);
var result = (from f in LMS_SurveyQuestion where innerQuery.Contains(f.SurveyQuestionID) && f.SurveyID = 1 select f);
Hope this will help.
try this
var result = from l in LMS_SurveyQuestion
let lsq = from l_S in LMS_SurveyQuestionOptionChoice
where l_S.NextPageNumber = 4
select l_S.SurveyQuestionID
where lsq.Contains(l.SurveyQuestionID) and l.surveyid = 1
select l.PageNumber;
I have the following query:
var query = (from wo in _dbContext.WorkOrder
join opr in _dbContext.Operation
on wo.operationID equals opr.operationID
where wo.orderid == selectedorderid
select new {wo.orderid, wo.workOrderID, wo.itemID, wo.operationID, opr.operationName, wo.operationCode}).ToList();
I also have another table,which joins with workorder table,and returns multiple values.
What I want to do is,I want to join the table and get a single column of it as a concentrated column in my query such as (id1,id2,id3) etc. How can I achieve this?
What about:
var query = (from wo in _dbContext.WorkOrder
join opr in _dbContext.Operation
on wo.operationID equals opr.operationID
where wo.orderid == selectedorderid
select new {wo.orderid, wo.workOrderID, wo.itemID, wo.operationID, opr.operationName, wo.operationCode}).ToList();
var orders = queryGroupBy(i => i.workOrderID)
.Select(i => new {WorkOrderId = i.workOrderID, ConcatinatedIds = String.Join(", ", i.Select(j => j.operationID))})
.ToList();
I have 2 records in tblMaterials and zero record in tblMaterialTenderGroups
But when I fetch the data to gridview it shows me the two records, and the join doesn't work
public List<tblMaterial> ShowPresentMaterialInGroup()
{
List<tblMaterial> q = (from i in dbconnect.tblMaterials.AsEnumerable()
join b in dbconnect.tblMaterialTenderGroups on i.materialId equals b.materialId
where b.MaterialGroupId == _materialGroupId
select new tblMaterial()
{
existAmout = i.existAmout,
materialId = i.materialId,
name = i.name,
needAmount = i.needAmount,
requestAmount = i.requestAmount,
unit = i.unit,
requestId = i.requestId
}).ToList();
return q;
}
Can you please try this
List<tblMaterial> q = from i in dbconnect.tblMaterials
join b in dbconnect.tblMaterialTenderGroups on i.materialId equals b.materialId
select new { existAmout = i.existAmout,
materialId = i.materialId,
name = i.name,
needAmount = i.needAmount,
requestAmount = i.requestAmount,
unit = i.unit,
requestId = i.requestId}.ToList();
May be using returning the two records..
I read something here
Using AsEnumerable will break off the query and do the "outside part"
as linq-to-objects rather than Linq-to-SQL. Effectively, you're
running a "select * from ..." for both your tables and then doing the
joins, where clause filter, ordering, and projection client-side.
I want to join two tables and want column of both tables in result using linq
I have something like
var k = (from t in Uow.Transactions.GetAllWithReferences()
join q in Uow.TransactionDetails.GetAll() on t.TransactionId equals q.TransactionId
select t)
Instead of just t u wabt columns of both t and q
Use anonymous object to select all columns from both tables:
var k = from t in Uow.Transactions.GetAllWithReferences()
join q in Uow.TransactionDetails.GetAll()
on t.TransactionId equals q.TransactionId
select new { t.Column1, t.Column2, q.Colum3 };
Or just select both entities in anonymous object:
var k = from t in Uow.Transactions.GetAllWithReferences()
join q in Uow.TransactionDetails.GetAll()
on t.TransactionId equals q.TransactionId
select new { Transaction = t, Details = q };
Or even better - use eager loading of details. Then code will look like:
var k = Uow.Transactions.GetAllIncluding(t => t.Details);
Using lambda syntax, create anonymous result containing both items:
var joinResult = Uow.Transactions.GetAllWithReferences()
.Join(Uow.TransactionDetails.GetAll(), transaction => transaction,
details => details, (transaction, details) => new
{
Transaction = transaction,
Details = details
});