I want to use Linq to ADO.NET to fetch all rows that match the criteria below from a DataTable.
Select all rows where "parentId" equals "id" of a row where "parentId" equals null.
Order by "Name".
Can someone tell me how to accomplish this (preferably using both Query Syntax and Method Syntax), and possibly point me to where I can read more about this topic?
There is no such thing as "Linq to ADO.NET" (perhaps you're confusing with ADO.NET Entity Framework). In your case, you seem to be referring to Linq to DataSets
You could do something like that :
Query syntax :
var parents = from row in table.AsEnumerable()
where row.IsNull("parentId")
select parents;
var children = from row in table.AsEnumerable()
where parents.Any(p => p.Field<int>("id") = row.Field<int>("parentId"))
orderby row.Field<string>("Name")
select row;
Method syntax :
var parents = table.AsEnumerable()
.Where(row => row.IsNull("parentId"));
var children = table.AsEnumerable()
.Where(row => parents.Any(p => p.Field<int>("id") = row.Field<int>("parentId")))
.OrderBy(row => row.Field<string>("Name"));
Related
I have tow tables - OrderRequisition and Order. I can show all the records from OrderRequisition table using linq query:
var list = (from r in db.OrderRequisition
select new SalesOrderViewModel
{
OrderId = r.OrderId ,
OrderNo = r.OrderNo
}).ToList();
I want to show only those records from OrderRequisition table which are not included in Order table. Any clue
Thanks
Partha
A simple approach that might be efficient enough because your database is able to optimize it:
var list = db.OrderRequisition
.Where(or => !db.Order.Any(o => o.OrderId == or.OrderId))
.ToList();
(skipped the SalesOrderViewModel initialization because not relevant for the question)
I'm trying to achieve this in c#
Select a.Name,a.Param
from Customization a
where a.name in (select Name from Standard)
I have try something like this but it still doesn't work.
merge = dt1.AsEnumerable()
.Where(r => r.Field<string>("Name")
.Contains(dt2.Rows.Contains("Name")))
.CopyToDataTable();
By using the current way we need to get the name list from the second data-table(dt2) for each row in dt1 so I suggest you get the list of names first and then check whether r.Field<string>("Name") contains in the collection. For this, you can use the following code
var NameCollection = dt2.AsEnumerable().Select(x=> x.Field<string>("Name")).ToList();
merge = dt1.AsEnumerable()
.Where(r => NameCollection.Contains(r.Field<string>("Name")))
.CopyToDataTable();
I have a query I know how to do in SQL but struggling to figure out the LINQ query. Here is the SQL.
SELECT ordNo, tranNo, COUNT(distinct custNo)
FROM orders
GROUP BY ordNo, tranNo
HAVING COUNT(distinct custNo) > 1
I don't feel like this is the same question that I see you marked as a duplicate. The linked question only groups on a single property. I've lost track of the Linq queries I've tried but here is one.
var countList = from o in orders
group o by new {o.orderNo, o.tranNo, o.custNo}
into grp
where grp.Key.custNo.Distinct().Count() > 1
select grp;
I tried the suggestion below but like someone commented you can't access the custNo property.
Just spitballing since I don't know the table structure.
context.orders
.GroupBy(o => new { o.ordNo, o.tranNo, o.custNo })
.Where(o => o.custNo.Distinct().Count() > 1)
.Select(o => new {
ordNo = o.ordNo,
tranNo = o.tranNo
});
I have the next query:
select VisitLines.ProcedureId, COUNT(DISTINCT VisitLines.VisitId) as nt
from Visits
LEFT JOIN VisitLines ON Visits.Id = VisitLines.VisitId
WHERE Visits.VisitStatusId = 1 AND Visits.IsActive = 1 AND VisitLines.IsActive = 1
GROUP BY VisitLines.ProcedureId
Main question: Does ability exists to grouping by column from join using linq ? I'm wondering how to do it using 'collection' column.
Is it possible to force EF to generate COUNT(DISTINCT column) ? IQueryable.GroupBy.Select(x => x.Select(n => n.Number).Distinct().Count()) generate query with few subqueries which much slower then COUNT(DISTINCT )
I found. Need to use SelectMany with second parameter resultSelector:
dbContext.Visits.Where(x => x.IsActive)
.SelectMany(x => x.VisitLines, (v, vl) => new
{
v.Id,
vl.ProcedureId
})
.GroupBy(x => x.ProcedureId)
.Select(x => new
{
Id = x.Key,
VisitCount = x.Count()
}).ToArray();
It generates the desired SQL, but with exception that I need distinct count by visit.
And if I change VisitCount = x.Distinct().Count() then EF generates a query with few subqueries again. But the main issue resolved
I have myself this SQL query
SELECT
db_accounts_last_contacts.id,
dbe_accounts_last_contacts.last_contact_date,
db_accounts_last_contacts.description,
db_accounts_last_contacts.follow_up_date,
db_accounts_last_contacts.spoke_to_person_id,
db_accounts_last_contacts.account_idFROM
db_accounts_last_contacts ,
db_companies
WHERE db_companies.id = db_accounts_last_contacts.account_id
ORDER BY db_accounts_last_contacts.last_contact_date DESC
Which returns my results ordered by last_contact_date.
Now I have my Entity framework query
var query = (from c in context.accounts_companies
select new AccountSearchResultModel()
{
LastContacted = (from calc in context.communique_accounts_last_contacts
where calc.account_id == companyId
orderby calc.last_contact_date descending
select calc.last_contact_date).FirstOrDefault()
});
However when I go ahead and do my ToList on it, my results are never ordered
Here is my table un-ordered
Here is my list ordered using the SQL query
Why isn't my entity framework query not picking up my orderby? Or if it is why am I always pulling out the first one?
You need to choose a Property to sort by and pass it as a lambda expression to OrderByDescending
like this:
.OrderByDescending(x => x.calc.last_contact_date);
I hope this helps.
Linq Orderby Descending Query
Sorry for the late answer,
What I had to do in the end was create a view and import it via the EDMX file and then use that to pull out my results.