I am using Following LINQ query to fetch data from datatable
var callBetweenNodesDetail = from r in dtRowCallBetweenNodes.AsEnumerable()
where ((r.Field<string>("F1") == VerSelected1) && (r.Field<string>("F2") == VerSelected2))
select r;
Now i wanna join another datatable dtRowFile that contains two fields "Name" and "F2" where field "F2" is to be matched with "F10" in datatable dtRowCallBetweenNodes to get "Name" in resultset
Would this help:
var ret = from p in Table1.AsEnumerable()
join q in Table2.AsEnumerable() on p.Field<int>("ID") equals q.Field<int>("ID") into UP
from q in UP.DefaultIfEmpty()
select new
{
ID = p.Field<int>("ID"),
Type = p.Field<string>("Type"),
Part = q.Field<int>("Part"),
Quantity = q.Field<int>("Quantity")
};
var rowFileNames = from r1 in dtRowCallBetweenNodes.AsEnumerable()
join r2 in dtRowFile.AsEnumerable()
on r1.Field<string>("F10") equals r2.Field<string>("F2")
where r1.Field<string>("F1") == VerSelected1
&& r1.Field<string>("F2") == VerSelected2
select r2.Field<string>("Name");
Cross-Table Queries (LINQ to DataSet)
Related
I have the following linq query that works fine, but I am wanting to pull one column (CompanyId) from context.Emps into the results along with the results from context.BillingProfiles. How would I modify the select (select prof) below to include said column?
var query = (from prof in context.BillingProfiles
join emp in context.Emps on prof.ID equals emp.ID
join grp in context.BillingGroups on prof.GroupID equals grp.GroupID
where (prof.EndDate == null) && (grp.System == "sysGrp") && (prof.ID == id)
select prof).Distinct()
.Select(x => new OpId()
{
id = x.ID,
GroupId = x.GroupID,
OpId = x.OpID,
StartDate = x.StartDate,
EndDate = x.EndDate,
AddedOn = x.AddedOn,
AddedBy = x.AddedBy,
RemovedOn = x.RemovedOn,
RemovedBy = x.RemovedBy,
Prodid = x.ProdID,
});
Thanks
Project an anonymous object containing those too:
var query = from prof in context.BillingProfiles
join emp in context.Emps on prof.ID equals emp.ID
join grp in context.BillingGroups on prof.GroupID equals grp.GroupID
where prof.EndDate == null && prof.ID == id && grp.System == "sysGrp"
select new { prof, emp.CompanyId, grp };
I am trying converting sql query into LINQ but after write query unable to fetch record from resultset
SELECT T.ServiceOrderNo,T.STATUS, T.SubStatus,T.orderVersion,T.OrderDate
,#pid, T.EventID, 'FOI'
FROM #temp1 T
LEFT JOIN Tbl_Service_Order_Progress O ON T.ServiceOrderNo DATABASE_DEFAULT = O.ServiceOrderNo
AND O.PARENTID = #pid
AND O.ServiceOrderType = 'FOI'
WHERE O.ServiceOrderNo IS NULL
Above Query following I'm trying in LINQ
var lstInsertFOI = (from i in lstFOI
join j in lstSOP on i.fulfilmentOrderItemIdentifier equals j.ServiceOrderNo into res
from subRight in res.DefaultIfEmpty()
where subRight.ParentId == parentId && subRight.ServiceOrderNo == null && subRight.ServiceOrderType.Equals("FOI")
select new
{
ServiceOrderNo = subRight.ServiceOrderNo == null ? i.fulfilmentOrderItemIdentifier : subRight.ServiceOrderNo,
EventStatus = i.status,
EventSubStatus = i.subStatus,
OrderVersion = i.orderVersion,
EVENTRECEIVEDDATE = i.orderDate,
ParentId = parentId,
EventID = i.eventID,
ServiceOrderType = "FOI",
}).ToList();
above linq query does not fetch expected result, which should return number of records from lstFOI list, but returns no record. Is the linq query correct?
Let start with SQL query.
LEFT JOIN Tbl_Service_Order_Progress O
ON T.ServiceOrderNo = O.ServiceOrderNo
AND O.PARENTID = #pid AND O.ServiceOrderType = 'FOI'
is equivalent to
LEFT JOIN (SELECT * FROM Tbl_Service_Order_Progress
WHERE PARENTID = #pid AND ServiceOrderType = 'FOI') O
ON T.ServiceOrderNo = O.ServiceOrderNo
then
WHERE O.ServiceOrderNo IS NULL
means that the query is actually using anti-join, i.e. include all the records from the left side that do not have a matching records from the right side.
With all that in mind, the equivalent LINQ query should be like this:
var lstInsertFOI = (
from i in lstFOI
join j in lstSOP
.Where(e => e.ParentId == parentId && subRight.ServiceOrderType == "FOI")
on i.fulfilmentOrderItemIdentifier equals j.ServiceOrderNo into res
where !res.Any()
select new
{
ServiceOrderNo = i.fulfilmentOrderItemIdentifier,
EventStatus = i.status,
EventSubStatus = i.subStatus,
OrderVersion = i.orderVersion,
EVENTRECEIVEDDATE = i.orderDate,
ParentId = parentId,
EventID = i.eventID,
ServiceOrderType = "FOI",
}).ToList();
I am using Entity framework 6.
I need to pass in a two table names and create a LINQ query that I will be able to use to populate a Kendo Datasource.
This is the code that I am currently trying to change to dynamic.
var entryPoint = (from pd in db.Employees
join od in db.MessageEmployees on pd.EmpID equals od.EmployeeID into t
from rt in t.DefaultIfEmpty()
where pd.Class == clssID && pd.IsInactive == 0
orderby pd.Class
select new
{
ClassID = pd.Class,
pd.FirstName,
pd.LastName,
pd.EmpID,
pd.EmployeeID,
EmpMessageID = rt.EmployeeID
});
//IQueryable<Employee> emps = db.Employees;
DataSourceResult result = entryPoint.ToDataSourceResult(request, empl => new HierachyEmployee
{
ClassID = empl.ClassID,
FirstName = empl.FirstName,
LastName = empl.LastName,
EmpID = empl.EmpID,
IsChecked = empl.EmpMessageID != null
});
return Json(result);
I need db.MessageEmployees and db.Employees to be dynamic. Thank you in advance for any help.
Hi I know this has been asked plenty of times, I'm not getting it through my skull
How to select Values from several tables
I made these two Linq queries
First
r = (from d in db.stageManagers
where d.profileID == UserID && d.verticalID == VerticalID
select new StageModels()
{
UserId = d.profileID,
VerticalId = (int)d.verticalID,
VerticalStageID = d.stageID
}).FirstOrDefault();
Second
r = (from d in db.stageManagerVerticals
where d.ID == r.VerticalId
select new StageModels()
{
VerticalName = d.verticalName
}
).FirstOrDefault();
I want to make them one statement since they add data to one model and the tables they query have a Pk Fk relationship
In the first block d.verticalId is what I use to get the value(Name) in the secondblock, d.verticalId is primary key to stageManagerVerticals and foreign key in stageManager, How can i join these queries ?
I tried this:
r = (from d in db.stageManagers
join c in db.stageManagerVerticals on d.stageID equals c.ID
where d.profileID == UserID && d.verticalID == VerticalID
select new StageModels()
{
UserId = d.profileID,
VerticalId = (int)d.verticalID,
VerticalStageID = d.stageID;
VerticalName = c.verticalName
}
).FirstOrDefault();
try with JOIN in LINQ to select values from more than one table
eg:
var innerJoinQuery =
from category in categories
join prod in products on category.ID equals prod.CategoryID
select new { ProductName = prod.Name, Category = category.Name };
var leftOuterJoin=(c in categories
join p in products on c.ID equals p.CategoryID into t
from temp in t.DefaultIfEmpty()
select new { ProductName = p.Name, Category = c.Name }
).ToList();
You can utilize the Navigational Properties,
var r = db.stageManagers.Where(x => x.profileID == UserID && x.verticalID == VerticalID).
Select(
d => new StageModels() {
UserID = d.profileID,
VerticalID = (int)d.verticalID,
VerticalStageID = d.stageID,
VerticalName = d.stageManagerVertical.verticalName
}
).FirstOrDefault();
I am trying to convert below SQL query into Linq.
select eg.userid, eg.groupid, count(r.RID) as RecipientCount
from eg
join g on eg.groupid = g.groupid
join r on g.RID = r.RID
where eg.UserId = '7F813844-3B93-418E-8141-654082C4E37D'
and eg.IsDeleted = 0
and r.Isdeleted = 0
group by eg.groupid
Above query runs properly in SQL.
My Linq code is:
var v = from eml in dc.egs
join recpingroup in dc.g on eml.GroupID equals recpingroup.GroupID
where eml.aspnet_User.LoweredUserName.Equals(strUserName.ToLower())
&& !eml.IsDeleted
&& !recpingroup.r.IsDeleted
select new Info()
{
CreateDt = eml.CreateDt.ToShortDateString(),
UserId = eml.UserId.ToString(),
LastUpdateDt = eml.LastUpdateDt.ToShortDateString(),
Username = eml.aspnet_User.UserName,
GroupDescription = eml.GroupDescription,
GroupID = eml.GroupID.ToString().ToUpper(),
GroupName = eml.GroupName,
Count = dc.g.Count(r1 => r1.GroupID.Equals(eml.GroupID) && !r1.r.IsDeleted)
};
where dc is my DataContext.
But I am having problems in the last property i.e. Count is coming wrong. I want the counts of recipients from recpingroup.r as RecipientCount.
Also note that tables are linked in SQL internally by PK and FK references.
try following
Count = (from x in dc.g where(r1 => r1.GroupID.Equals(eml.GroupID) && !r1.r.IsDeleted) select x.g).count()