This is mysql query:
SELECT count(PVersion), PVersion
FROM [Products].[dbo].[Active_Details]
group by PVersion
order by count(PVersion);
What will be its LINQ to SQL.
Try this:
var product =
from p in yourContext.Active_Details
group p by p.PVersion into pgroup
let count = pgroup.Count()
orderby count
select new { Count = count, PVersion = pgroup.Key };
SELECT count(ProductVersion), ProductVersion , ProductID , SubProductID
FROM [do-not-delete-accounts].[dbo].[Activation_Details]
group by ProductVersion,ProductID,SubProductID
order by count(ProductVersion);
var query =
from p in yourContext.Activation_Details
group p by new
{
ProductVersion = p.ProductVersion,
ProductID = p.ProductID,
SubProductID = p.SubProductID
}
into pgroup
let count = pgroup.Count()
orderby count
select new
{
Count = count,
ProductVersion = pgroup.Key.ProductVersion,
ProductID = pgroup.Key.ProductID,
SubProductID = pgroup.Key.SubProductID
};
Should be a group into:
var product = (
from p in yourContext.Active_Details
group p by p.PVersion into pgroup
select new { VersionCount= pgroup.Count(), pgroup.Key }
).OrderBy(x=>x.VersionCount);
Here is a MSDN Resource with examples
Related
How to convert the following query into linq
SELECT
a.ProductId,
a.Name,
a.Description,
b.Quoteid,
b.Productid,
b.Quantity,
b.OriginalPrice
FROM
Products AS a
LEFT JOIN
QuoteDtails AS b
ON a.ProductId = b.ProductId
AND b.QuoteId = 200;
Don't know where to add the AND condition.
Thanks and regards
You can try this linq if you want to write LEFT JOIN of linq, you need to add
into [temp collection] from [Left join talbe collection] in [temp collection].DefaultIfEmpty()
after Linq join
look like this.
from ss in Products
join aa in QuoteDtails
on ss.ProductId equals aa.ProductId into temp
from ds in temp.DefaultIfEmpty()
where ds.QuoteId = 200
select new
{
ProductId_P = ss.ProductId,
Name = ss.Name,
Description = ss.Description,
Quoteid = ds.Quoteid,
Productid_Q = ds.Productid,
Quantity = ds.Quantity,
OriginalPrice = ds.OriginalPrice
}
You can add AND condition in your LINQ query like this :
var res = from p in products
join q in quoteDtails on new { Criteria1 = p.ProductID, Criteria2 = 200 } equals new { Criteria1 = q.Productid, Criteria2 = q.Quoteid }
select new
{
ProductId_P = p.ProductID,
Name = p.Name,
Description = p.Description,
Quoteid = q.Quoteid,
Productid_Q = q.Productid,
Quantity = q.Quantity,
OriginalPrice = q.OriginalPrice
};
I have tow tables, tblItem and tblInsertLines, in tblInsertLines I have the same ItemId but with differnt ProdDate and ExpireDate, I want to get a distinct list of all items but select the first row from tblInsertLines as the first row contains the oldest ProdDate.
Any help will be appreciated. I use this code.
public static List<Item> getItemList()
{
using (var db = new AWarehouseDataClassesDataContext())
{
var list = (from i in db.tblItems
join e in db.tblInsertLines on i.ItemId equals e.ItemId
orderby i.NameE
select new Item
{
code = i.Code,
itemId = i.ItemId,
lastUpdate = i.LastUpdate,
nameA = i.NameA,
nameE = i.NameE,
qty = i.Qty,
prodDate = e.ProdDate,
expireDate = e.ExpireDate,
updatedBy = i.UpdatedBy
}).Distinct();
return list.ToList();
}
}
You can try
var list= (from i in db.tblItems
join e in db.tblInsertLines on i.ItemId equals e.ItemId
where e.counter > 0
orderby i.NameE
group new { i, e } by e.ItemId into g
select new Item
{
code = g.First().i.Code,
itemId = g.Key,
lastUpdate = g.First().i.LastUpdate,
nameA = g.First().i.NameA,
nameE = g.First().i.NameE,
qty = g.First().i.Qty,
prodDate = g.Min(x=>x.e.ProdDate),
expireDate = g.First().e.ExpireDate,
updatedBy = g.First().i.UpdatedBy
}).ToList();
Here the query for retrieving all the comptes in my base.
I would like to add a number within the object GridCompte in order to count the element inside the list (1,2,3...):
var comptes = (from c in Comptes
join s in Societies on c.IdSoc equals s.IdSoc
select new GridCompte
{
SocCompteId = c.IdCompte,
Name = c.Name,
Nb = ??? COUNT ???,
.....
SocName = s.Name
}).ToList();
I tried using the group statement, but i didn't manage to achieve my goal.
Any suggestions?
First prepare your linq only for the fields you want to get
var comptes = from c in Comptes
join s in Societies on c.IdSoc equals s.IdSoc
select new
{
SocCompteId = c.IdCompte,
Name = c.Name,
.....
SocName = s.Name
};
Now use index option that is available in Select
var finalComptes = (comptes.AsEnumerable()
.Select((comptes, index) => new GridCompte()
{
SocCompteId = c.IdCompte,
Name = c.Name,
Nb = index + 1,
.....
SocName = s.Name
}).ToList();
I have a Sql Query like :
select distinct FROM_EMAILID,FROM_COUNTRY from SURVEY_VISITORS
where FROM_COUNTRY IN
(
select top 1 FROM_COUNTRY as FROM_COUNTRY from SURVEY_VISITORS
where FROM_COUNTRY<>'undefined'
group by FROM_COUNTRY order by COUNT(*) desc
)
I'm unable to handle with IN Operator.
Any help would be greatly appreciated.
For the sub-Query,I've tried this way :
var innerQuery = (from t in VDC.SURVEY_VISITORS
group t by new
{
t.FROM_COUNTRY
} into g
orderby
g.Count() descending
select new
{
VisitorCount = (Int64?)g.Count(),
Country = g.Key.FROM_COUNTRY
}).FirstOrDefault();
var innerQuery = (from t in VDC.SURVEY_VISITORS
group t by new
{
t.FROM_COUNTRY
} into g
orderby
g.Count() descending
select new
{
VisitorCount = (Int64?)g.Count(),
Country = g.Key.FROM_COUNTRY
}).FirstOrDefault();
var result = (from xx in VDC.SURVEY_VISITORS
where ((innerQuery.Country.Contains(xx.FROM_COUNTRY)) && xx.TEMPLATE_ID == RecentBlastedTemplate.TEMPLATE_ID)
select new
{
xx.FROM_COUNTRY,
xx.FROM_EMAILID
}).Distinct().ToList();
Is it possible to generate the following SQL query by using LINQ-to-SQL query expression or method chains which is defer-executable?
Data Structure
alt text http://www.freeimagehosting.net/uploads/e062a48837.jpg
Select Distinct ClassRoomTitle,
Count(*) Over(Partition By ClassRoomNo) As [No Sessions Per Room],
TeacherName,
Count(*) Over(Partition By ClassRoomNo, TeacherName) As [No Sessions Per Teacher] From ClassRoom
Expected Result
alt text http://www.freeimagehosting.net/uploads/47a79fea8b.jpg
Try this:
var vGroup = from p in ClassRoom
group p by new { p.ClassRoomNo, p.TeacherName }
into g
from i in g
select new
{
i.ClassRoomNo,
i.TeacherName,
i.ClassRoomTitle,
NoSessionsPerTeacher = g.Count()
};
var pGroup = from p in vGroup
group p by new { p.ClassRoomNo }
into g
from i in g
select new
{
i.ClassRoomTitle,
NoSessionsPerRoom = g.Count(),
i.TeacherName,
i.NoSessionsPerTeacher
};
var result = pGroup.OrderBy(p => p.ClassRoomNo).ThenBy(p => p.TeacherName);
I didn't test the above but you can check my original code in case I got something wrong in the rewrite:
var vGroup = from p in Products
group p by new { p.ProductId, p.VariantId }
into g
from i in g
select new
{
i.ProductId,
i.VariantId,
VariantCount = g.Count()
};
var pGroup = from p in vGroup
group p by new { p.ProductId }
into g
from i in g
select new
{
i.ProductId,
ProductCount = g.Count(),
i.VariantId,
i.VariantCount
};
var result = pGroup.OrderBy(p => p.ProductId).ThenBy(p => p.VariantId);
var classRooms = from c in context.ClassRooms
group c by new {c.ClassRoomNo} into room
select new {
Title = room.First().ClassRoomTitle,
NoSessions = room.Count(),
Teachers = from cr in room
group cr by new {cr.TeacherName} into t
select new {
Teacher = t.Key,
NoSessions = t.Count()
}
};
A bit more structured than the posted expected result, but I find that to be better.
You can always use SelectMany if you want to go back to unstructured:
var unstructured = classRooms
.SelectMany(c=> c.Teachers.Select( t=> new {
Title = c.Title,
SessionsPerRoom = c.NoSessions,
Teacher = t.Teacher,
SessionsPerTeacher = t.NoSessions
});