I have this Linq query which works fine
var test = (from c in context.ConsumptionSet
join pi in context.PropertiesInstanceSet on c.PropertiesInstanceID equals pi.PropertiesInstanceID
join ep in context.EquipmentPropertiesSet on pi.EquipmentPropertiesID equals ep.EquipmentPropertiesID
where (ep.EquipmentID == equipmentId && pi.ProprietesName == ProprietesName.Energy && c.Date <= DateTime.Today && c.Date >= EntityFunctions.AddDays(DateTime.Today, -7))
group c by c.Date.Day into grp
select new
{
test = grp.Key,
value = (from c2 in grp select c2.Value).Max()
}).ToList();
But I want to group these results by the DayOfWeek Property, and it seems like Linq doesn't allow it since I get this error when I replace group c by c.Date.Day by group c by c.Date.DayOfWeek :
The specified type member 'DayOfWeek' is not supported in LINQ to Entities
Is there any workaround to this problem ?
Use SqlFunctions.DatePart:
var test = (from c in context.ConsumptionSet
join pi in context.PropertiesInstanceSet on c.PropertiesInstanceID equals pi.PropertiesInstanceID
join ep in context.EquipmentPropertiesSet on pi.EquipmentPropertiesID equals ep.EquipmentPropertiesID
where (ep.EquipmentID == equipmentId && pi.ProprietesName == ProprietesName.Energy && c.Date <= DateTime.Today && c.Date >= EntityFunctions.AddDays(DateTime.Today, -7))
group c by SqlFunctions.DatePart("weekday", c.Date) into grp
select new
{
test = grp.Key,
value = (from c2 in grp select c2.Value).Max()
}).ToList();
Related
Can anyone help with how to transform this LINQ query syntax to method syntax.
It is because i need to use the Take() method.
IQueryable<QueueItem> rs = from a in ldb.QueueItems
join b in ldb.Robots on a.RobotId equals b.Id
join c in ldb.RobotsXEnvironments on b.Id equals c.RobotId
join d in ldb.Environments on c.EnvironmentId equals d.Id
join e in ldb.Releases on d.Id equals e.EnvironmentId
where e.ProcessKey == dropdown || a.Reference == query ||
a.SpecificData.Contains(query) &&
a.StartProcessing >= fromDate && a.EndProcessing <= toDate
select a;
You can do something like this:
IQueryable<QueueItem> rs = (from a in ldb.QueueItems
join b in ldb.Robots on a.RobotId equals b.Id
join c in ldb.RobotsXEnvironments on b.Id equals c.RobotId
join d in ldb.Environments on c.EnvironmentId equals d.Id
join e in ldb.Releases on d.Id equals e.EnvironmentId
where e.ProcessKey == dropdown || a.Reference == query ||
a.SpecificData.Contains(query) &&
a.StartProcessing >= fromDate && a.EndProcessing <= toDate
select a).take(number);
I have a array called searchWords, that is a dynamic array that stores peoples search words. I need to add an option for AND search. So the search will only retrieve items if both variables in searchWords contains for resultList. Now it is searchWords.Any. Will searchWords.All make this works?
var resultList = from c in context.Category
join q in context.Question on c.CategoryId equals q.CategoryId
join qf in context.QuestionFilter on q.QuestionId equals qf.QuestionId
join a in context.Answer on q.QuestionId equals a.QuestionId into QuestAnsw
from a2 in QuestAnsw.DefaultIfEmpty()
orderby c.SortOrder
orderby q.SortOrder
where qf.FilterId == filterKeyAsInt
&& q.Published == true
&& c.Published == true
&& q.CustomerId == customerId
&& (searchWords.Any(w => a2.Text.Contains(w))
|| searchWords.Any(w => c.Text.Contains(w))
|| searchWords.Any(w => q.Text.Contains(w)))
select new { Category = c, Question = q };
You can put multiple clauses inside an All(), e.g.
&& (searchWords.All(w =>
a2.Text.Contains(w) &&
c.Text.Contains(w) &&
q.Text.Contains(w)))
...
You can do this if use searchWords.All, but i think searchWords.Any is more intuitive.
var resultList = from c in context.Category
join q in context.Question on c.CategoryId equals q.CategoryId
join qf in context.QuestionFilter on q.QuestionId equals qf.QuestionId
join a in context.Answer on q.QuestionId equals a.QuestionId into QuestAnsw
from a2 in QuestAnsw.DefaultIfEmpty()
orderby c.SortOrder
orderby q.SortOrder
where qf.FilterId == filterKeyAsInt
&& q.Published == true
&& c.Published == true
&& q.CustomerId == customerId
&& !
(
searchWords.All(w => !a2.Text.Contains(w))
&& searchWords.All(w => !c.Text.Contains(w))
&& searchWords.All(w => !q.Text.Contains(w))
)
select new { Category = c, Question = q };
I have the LINQ to SQL as below, which works fine.
var qry = (from h in dc.Timesheets
join s in dc.Users on h.UserID equals s.UserID
join ug in dc.UserGroups on s.UserGroupID equals ug.UserGroupID
where h.BookedOn >= _dateFrom.Value && h.BookedOn <= _dateTo.Value
group h by ug.UserGroupID into g
orderby g.Count() descending
select new
{
UserGroupingID = g.Key,
BookingsPerDay = g.Count() / days
}).ToList();
Now I want to add the name of the User Group, but somehow I struggle to get the LINQ right.
My limited knowledge tells me I should add the Description to the Group clause as follow, but it's a no-go:
I try:
var qry = (from h in dc.Timesheets
join s in dc.Users on h.UserID equals s.UserID
join ug in dc.UserGroups on s.UserGroupID equals ug.UserGroupID
where h.BookedOn >= _dateFrom.Value && h.BookedOn <= _dateTo.Value
group h, GroupDescription = ug.Description by ug.UserGroupID into g
orderby g.Count() descending
select new
{
UserGroupingID = g.Key,
Description = g.Key.GroupDescription
BookingsPerDay = g.Count() / days
}).ToList();
Error: Cannot convert lambda expression to type
'System.Collections.Generic.IEqualityComparer' because it is not
a delegate type
I think you might missing the new keyword here!!
var qry = (from h in dc.Timesheets
join s in dc.Users on h.UserID equals s.UserID
join ug in dc.UserGroups on s.UserGroupID equals ug.UserGroupID
where h.BookedOn >= _dateFrom.Value && h.BookedOn <= _dateTo.Value
group new {h, GroupDescription = ug.Description} by new {GroupDescription} into g
orderby g.Count() descending
select new
{
UserGroupingID = g.Key,
Description = g.Key.GroupDescription
BookingsPerDay = g.Count() / days
}).ToList();
Shouldn't it be an == for the GroupDescription? And aren't you missing a comma in the select new statement?
I want to sum records using Group by from the all data inside "result view".
can anyone guide me for this.!
Here is My Code
var tData1 = (from i in _data.Transactions
join y in _data.DeviceInformations on i.DeviceInfoId equals y.DeviceInfoId
join u in _data.AccountDevices on y.DeviceInfoId equals u.DeviceInfoId
where y.Active == true && u.AccountId == 1000001 && u.Active == true
group i by i.DeviceInfoId into g
select g.OrderByDescending(t => t.DateCreated)).ToList();
foreach (var xCo in tData1)
{
//I am getting Data in xCo
}
Based on #Nayeem Mansoori solution you can try this.
var tData1 = (from i in _data.Transactions
join y in _data.DeviceInformations on i.DeviceInfoId equals y.DeviceInfoId
join u in _data.AccountDevices on y.DeviceInfoId equals u.DeviceInfoId
where y.Active == true && u.AccountId == 1000001 && u.Active == true
group i by i.DeviceInfoId into g
select new {Id = DeviceInfoId, sum = g.Sum(x=>x.DeviceInfoId)};
I have one query in SQL, want to convert it to Linq. My Query is :
select count(tbs.tbsid) as CNTSeats,tbm.BusNo
from tdp_tourpackageschedule tps
join tdp_tourpackage tp on tp.TourID = tps.FK_TourID
join tdp_tourbusseats tbs on tbs.tbsid = tps.fk_tbsid
join tdp_tourbusmaster tbm on tbm.tbid = tbs.fk_tbid
where fk_tdid = #FKTDID and fk_TourID = #FKTourID and IsOpen = 1
group by tbm.BusNo
I tried this Code:
var tourAvail = (from ts in entities.tdp_TourPackageSchedule
join tp in entities.tdp_TourPackage on ts.FK_TourID equals tp.TourID
join tbs in entities.tdp_TourBusSeats on ts.FK_TBSID equals tbs.TBSID
join tb in entities.tdp_TourBusMaster on tbs.FK_TBID equals tb.TBID
where ts.FK_TDID == TDID && ts.FK_TourID == TourID && ts.IsOpen == 1
group tb by tb.BusNo into cnt
select new
{
//BusNo = cnt.bu
//Count = cnt.Select(x => x.tdp_TourBusSeats).Distinct().Count()
});
I don't know how to get count of records, anyone can help ?
Do :
var tourAvail = (from ts in entities.tdp_TourPackageSchedule
join tp in entities.tdp_TourPackage on ts.FK_TourID equals tp.TourID
join tbs in entities.tdp_TourBusSeats on ts.FK_TBSID equals tbs.TBSID
join tb in entities.tdp_TourBusMaster on tbs.FK_TBID equals tb.TBID
where ts.FK_TDID == TDID && ts.FK_TourID == TourID && ts.IsOpen == 1
group tb by tb.BusNo into cnt
select new
{
BusNo = cnt.Key,
Count = cnt.Count()
}).ToList();