I have a linq script
var ID = (from item in ConflictDatas.AsEnumerable()
group item by new
{
ID = item.Field<string>("ID"),
DesignArticle = item.Field<string>("DesignArticle"),
DesignNo = item.Field<string>("DesignNo"),
PatternCode = item.Field<string>("PatternCode")
} into g
where g.Count() >= 2
select new
{
g.Key.ID
}).ToList();
I want to put this result into a sql commnad.
I try:
string sqlwhere;
sqlwhere = string.Join(",", ID);
tsql = #"
Insert ConflictDesignArticle
Select * from ReadyworkData where ID in (" + sqlwhere + #") ";
After compile:
Insert ConflictDesignArticle
Select * from ReadyworkData where ID in ({ ID = SPSOS17040113 },{ ID =
SPSOS17040115 },{ ID = SPSOS17040114 })
How to modify my code. Thanks.
Thank you for Lei Yang help
var ID = (from item in ConflictDatas.AsEnumerable()
group item by new
{
ID = item.Field<string>("ID"),
DesignArticle = item.Field<string>("DesignArticle"),
DesignNo = item.Field<string>("DesignNo"),
PatternCode = item.Field<string>("PatternCode")
} into g
where g.Count() >= 2
select new
{
g.Key.ID
}).Select(x => x.ID).ToList();
Related
I have a Combobox which gets its data from my database.
var people = (from x in db.Person select new { Value = x.Id, Names = x.Namn + " " + x.EfterNamn }).ToList();
cbpeople.DataSource = people;
cbpeople.DisplayMember = "Names";
cbpeople.ValueMember = "Value";
cbpeople.SelectedIndex = -1;
And I have the SelectedIndex function
int id = cbpeople.SelectedIndex + 1;
string namn = (from x in db.Person where x.Id == id select x.Namn).ToString();
lblNamn.Text = namn;
So as you can see, I'm trying to have it select the information from the same row in the database and put them in labels. (The "cbpeople.SelectedIndex + 1;" is because I had no other way to get the ID from the SelectedValue).
But all it prints out is this long thing instead of the Name (on the label)
"SELECT \r\n [Extent1].[Namn] AS [Namn]\r\n FROM [dbo].[Person] AS [Extent1]\r\n WHERE [Extent1].[Id] = #p__linq__0"
What am I doing wrong?
You calling ToString() over IQueryable object. Of course, it will return it's SQL representation. To execute query you can do this:
string namn = (from x in db.Person where x.Id == id select x.Namn).Single();
I need to sum elements of same type starting from 2 LINQ queries.
Below is my code:
var query1 = from d in _contextProvider.Context.Documents
where d.TransportId == transportId
group d by d.Type
into dg
select new { DocumentType = dg.Key.ToString(), DocumentCount = dg.Count() };
var query2 = from n in _contextProvider.Context.NotificationDocuments
where n.TransportId == transportId
group n by n.TransportId
into nd
select new { DocumentType = "Notification", DocumentCount = nd.Count() };
var query_collapsed = query1.Union(query2)
.GroupBy(p => new { DocumentType = p.DocumentType })
.Select(g => new DocumentCounters() { DocumentType = g.Key.DocumentType, DocumentCount = g.Sum(p => p.DocumentCount) });
Example: below let's analyse values for DocumentType equals to Notification.
Values of query1:
Values of query2:
The collapsed query :
That's correct: 1 + 2 = 3
The problem: I noticed that whenever the count for Notification in query1 is equals to the count for Notification in query2, then the sum is not performed.
Example:
2 + 2 = 2
or
3 + 3 = 3
Any ideas ?
LINQ Union will remove duplicate entries. If you want to merge the two sequences you can use Concat like so:
var query_collapsed = query1.Concat(query2)
.GroupBy(p => new { DocumentType = p.DocumentType })
.Select(g => new DocumentCounters() { DocumentType = g.Key.DocumentType, DocumentCount = g.Sum(p => p.DocumentCount) });
This question already has answers here:
C# Using LINQ to select max ID of row
(6 answers)
Closed 8 years ago.
I'm trying to select the max value of BreadCrumbID from the table tbBreadCrumb when I return multiple rows. Right now I join the tabel tbBreadCrumb to the table tbProjects. If there are multiple entries in tbBreadCrumb I return the multiple entries. But I only want to select the row in tbBreadCrumb that has the highest BreadCrumbID.
Here's my code:
using (dbPSREntities5 myEntities = new dbPSREntities5())
{
var allDepartments = (from tbProject in myEntities.tbProjects
from tbBreadCrumb in myEntities.tbBreadCrumbs.Where(x => x.ProjectID == tbProject.ProjectID) <--- I want to put some sort of Max() function here where it will only slect the row with the max BreadCrumID in the table tbBreadCrumb
from refBreadCrumb in myEntities.refBreadCrumbs.Where(x => x.refBreadCrumbID == tbBreadCrumb.StatusID)
// select new anon type
select new
{
ProjectID = tbProject.ProjectID,
Status = refBreadCrumb.BreadCrumbValue,
DateSubmitted = tbBreadCrumb.CreateDateTime,
refDepartmentID = tbProject.refDepartmentID,
ProjectContactFullName = tbProject.ProjectContactFirstName + " " + tbProject.ProjectContactLastName,
ProjectWorkType = tbProject.ProjectWorkType,
});
projectsListView.DataSource = allDepartments;
projectsListView.DataBind();
}
You can try this,first get the Maximum BreadCrumbID Id.Then use it with where in the second query :
var breadId = myEntities.tbBreadCrumbs.Max(x => x.BreadCrumbID);
var allDepartments = (from tbProject in myEntities.tbProjects
from tbBreadCrumb in myEntities.tbBreadCrumbs.Where(x => x.ProjectID == tbProject.ProjectID && x.BreadCrumbID == breadId)
from refBreadCrumb in myEntities.refBreadCrumbs.Where(x => x.refBreadCrumbID == tbBreadCrumb.StatusID)
// select new anon type
select new
{
ProjectID = tbProject.ProjectID,
Status = refBreadCrumb.BreadCrumbValue,
DateSubmitted = tbBreadCrumb.CreateDateTime,
refDepartmentID = tbProject.refDepartmentID,
ProjectContactFullName = tbProject.ProjectContactFirstName + " " + tbProject.ProjectContactLastName,
ProjectWorkType = tbProject.ProjectWorkType,
});
Update:
var allDepartments = (from tbProject in myEntities.tbProjects
from tbBreadCrumb in myEntities.tbBreadCrumbs.Where(x => x.ProjectID == tbProject.ProjectID && x.BreadCrumbID == myEntities.tbBreadCrumbs.Where(y => y.ProjectID == tbProject.ProjectID).Max(y => y.BreadCrumbID))
from refBreadCrumb in myEntities.refBreadCrumbs.Where(x => x.refBreadCrumbID == tbBreadCrumb.StatusID)
// select new anon type
select new
{
ProjectID = tbProject.ProjectID,
Status = refBreadCrumb.BreadCrumbValue,
DateSubmitted = tbBreadCrumb.CreateDateTime,
refDepartmentID = tbProject.refDepartmentID,
ProjectContactFullName = tbProject.ProjectContactFirstName + " " + tbProject.ProjectContactLastName,
ProjectWorkType = tbProject.ProjectWorkType,
});
i have table
id pagenane username
i want this sql query
select pagename, count(*) as num from pagestat where username='name' group by pagename
how can i do it by linq?
Well, try this:
var query = from row in context.pageStat
where row.UserName == "name"
group row by row.PageName into g
select new { PageName = g.Key, Count = g.Count() };
var retVal = (from s in dataContext.YourTable where
s.UserName.Equals("name")
group s by s.PageName into k
select new {PageName = k.Key, Count = k.Count() }).ToList();
I've got a T-SQL query similar to this:
SELECT r_id, r_name, count(*)
FROM RoomBindings
GROUP BY r_id, r_name
I would like to do the same using LINQ. So far I got here:
var rooms = from roomBinding in DALManager.Context.RoomBindings
group roomBinding by roomBinding.R_ID into g
select new { ID = g.Key };
How can I extract the count(*) and r_name part?
Try this:
var rooms = from roomBinding in DALManager.Context.RoomBindings
group roomBinding by new
{
Id = roomBinding.R_ID,
Name = roomBinding.r_name
}
into g
select new
{
Id = g.Key.Id,
Name = g.Key.Name,
Count = g.Count()
};
Edit by Nick - Added method chain syntax for comparison
var rooms = roomBinding.GroupBy(g => new { Id = g.R_ID, Name = g.r_name })
.Select(g => new
{
Id = g.Key.Id,
Name = g.Key.Name,
Count = g.Count()
});