I'm taking a list of counts, averaging and returning all results according to the condition. Looks like my command is too erroneous. I don't know how to do it yet.
I have tables:
var qr = (from Product c in _context.Products
join o in _context.RatingProducts on c.ID equals o.IDProduct
where c.Status == true
orderby c.NumProduct
group o by c.ID into g
select new {
ProductId = g.Key,
CountID = g.Count(),
//SumID = g.Sum(s => s.StarRating).ToString(),
AveragedID = g.Count() > 0 ? g.Average(x => x.StarRating) : 0
}).ToList();
var getqr = _context.Products.Where(a => a.Status).ToList();
int count = 0;
double averaged = 0;
foreach (var item in getqr)
{
int key = item.ID;
var id = qr.Where(x => x.ProductId == key).FirstOrDefault();
if(id != null)
{
count = id.CountID;
averaged = Math.Round(id.AveragedID,1);
}
var productquery = item;
}
var query = from c in qr
join p in getqr on c.ProductId equals p.ID
select new ProductQuery
{
ID = p.ID,
IDProduct = p.IDProduct,
CategoryID = p.CategoryID,
NumProduct = p.NumProduct,
Name = p.Name,
Status = p.Status,
Price = p.Price,
PriceOld = p.PriceOld,
ContentsSmall = p.ContentsSmall,
Contents = p.Contents,
Images = p.Images,
CountID = count,
//SumID = c.SumID,
AveragedID = averaged,
};
return query.ToList();
However when I debug, it shows only one result with row ID = 1. I want to display list: count, average results of table 1. Tks.
As far as I understand you should make LEFT JOIN here, so need to add DefaultIfEmpty in the initial query, and slightly change average counting logic.
Something like this, should work:
var qr = (from Product c in _context.Products
join o in _context.RatingProducts on c.ID equals o.IDProduct into productRatings
from pr in productRatings.DefaultIfEmpty()
where c.Status == true
orderby c.NumProduct
group o by c.ID into g
select new {
ProductId = g.Key,
CountID = g.Count(),
AveragedID = g.Average(x => x == null ? 0 : x.StarRating)
}).ToList();
Related
I tried delete a statement that use contain() and use skip() take()
but it's still take a long time on execution
public JsonNetResult GetList(C.Lib.ListParam param, string type, DateTime? WorkDate, string MNo, string ANo, string AName, string Specifi, string PNo, string SName, string PType, string OrNo)
{
var idObj = (mis.Models.Security.CIdentity)Csla.ApplicationContext.User.Identity;
var c = new mis.Models.CEntities();
var targetList = (from obj in c.WorkOrders
join obj2 in c.Partners on obj.PartnerId equals obj2.PartnerId
join obj3 in c.Assets on obj.AssetsId equals obj3.AssetsId
join obj4 in c.WorkOrderItems on obj.WorkOrderId equals obj4.WorkOrderId
join obj5 in c.WorkItems on obj4.WorkId equals obj5.WorkId
join obj6 in c.Products on obj3.AssetsId equals obj6.AssetsId
where true && obj.TenantId == idObj.TenantId && obj2.Customer == true && obj.WorkOrderStatus <= 1
select new
{
WorkProgressId = obj.WorkOrderId,
OrderNo = obj.InOrderItem.OrderItem.Order.OrderNo,
ProductType = obj5.ProductType,
WorkOrderId = obj.WorkOrderId,
MakeQty = obj.MakeQty,//delete some column
PrepaidDate = obj.PrepaidDate,
CompleteDate = (from subobj in c.WorkOrderItems
where subobj.WorkOrderId == obj.WorkOrderId && subobj.CompleteDate != null
orderby subobj.CompleteDate descending, subobj.WorkItem.WorkNo descending
select subobj.CompleteDate).FirstOrDefault(),
WorkNoName = (from subobj in c.WorkOrderItems
where subobj.WorkOrderId == obj.WorkOrderId && subobj.CompleteDate != null
orderby subobj.WorkItem.WorkNo descending
select new { WorkNoName = subobj.WorkItem.WorkNo + " " + subobj.WorkItem.WorkName }).FirstOrDefault().WorkNoName,
});
targetList = targetList.GroupBy(x => x.MakeNo).Select(x => new
{
WorkProgressId = x.FirstOrDefault().WorkOrderId,
OrderNo = x.FirstOrDefault().OrderNo,
ProductType = x.FirstOrDefault().ProductType,
WorkOrderId = x.FirstOrDefault().WorkOrderId,
WorkOrderDate = x.FirstOrDefault().WorkOrderDate,
OrderQty = x.FirstOrDefault().OrderQty,
MakeQty = x.FirstOrDefault().MakeQty,
PrepaidDate = x.FirstOrDefault().PrepaidDate,//delete some column
CompleteDate = x.FirstOrDefault().CompleteDate,
WorkNoName = x.FirstOrDefault().WorkNoName
}).OrderByDescending(x => x.WorkOrderDate).ThenByDescending(x => x.WorkNoName);
param.SetCount(targetList);
targetList.OrderByDescending(x => x.WorkOrderDate).ThenByDescending(x=> x.WorkNoName);
var tk= targetList.Skip((param.Page - 1) * param.Rows).Take(param.Rows);
return JsonNetHelper.ReturnData(param,tk);
}
I'm wondering what is the best way to optimize it? Or would the query profiler do that for me?
I've this linq query:
var query = (from l in _contexto.lineasencargos
join a in _contexto.articulos on l.IDARTICULO equals a.IDARTICULO
join af in _contexto.articulofamilia on a.IDARTICULO equals af.IDARTICULO
join f in _contexto.familias on af.IDFAMILIA equals f.IDFAMILIA
join e in _contexto.encargos on l.IDENCARGO equals e.IDENCARGO
where e.FECHAHORAENCARGOS >= _finder.FechaDe &&
e.FECHAHORAENCARGOS <= _finder.FechaA &&
e.FECHAHORARECOGERENCARGOS >= _finder.FechaRecogerDe &&
e.FECHAHORARECOGERENCARGOS <= _finder.FechaRecogerA &&
e.clientes.RAZONSOCIALCLIENTE.Contains(_finder.Cliente)
group l by new { l.IDARTICULO, l.CANTIDADLINEAENCARGO,a.DESCRIPCIONARTICULO,f.DESCRIPCION,f.IDFAMILIA }
into g
select new listaEncargosAgrupados
{
IdArticulo=(int)g.Key.IDARTICULO,
DescripcionArticulo=g.Key.DESCRIPCIONARTICULO,
IdFamilia=g.Key.IDFAMILIA,
DescripcionFamilia=g.Key.DESCRIPCION,
SumaCantidad = (decimal)g.Sum(x => x.CANTIDADLINEAENCARGO),
SumaPrecio = (decimal)g.Sum(x => x.PRECIOLINEAENCARGO),
Total = (decimal)((decimal)g.Sum(x => x.CANTIDADLINEAENCARGO) * g.Sum(x => x.PRECIOLINEAENCARGO))
});
and i need create a condition in Where, that filter dynamically:
if (_finder.IdTienda > 0)
{
query = query.Where(x=>x.IDTIENDA == _finder.IdTienda);
}
But this Where it is not correct because IDTIENDA is contained in _context.encargos and not in listaEncargosAgrupados
How i can revolve this problem?
Thanks
Add e into grouping statement and IDTIENDA list into the result:
var query = (from l in _contexto.lineasencargos
join a in _contexto.articulos on l.IDARTICULO equals a.IDARTICULO
join af in _contexto.articulofamilia on a.IDARTICULO equals af.IDARTICULO
join f in _contexto.familias on af.IDFAMILIA equals f.IDFAMILIA
join e in _contexto.encargos on l.IDENCARGO equals e.IDENCARGO
where e.FECHAHORAENCARGOS >= _finder.FechaDe &&
e.FECHAHORAENCARGOS <= _finder.FechaA &&
e.FECHAHORARECOGERENCARGOS >= _finder.FechaRecogerDe &&
e.FECHAHORARECOGERENCARGOS <= _finder.FechaRecogerA &&
e.clientes.RAZONSOCIALCLIENTE.Contains(_finder.Cliente)
group new { l, e} by new { l.IDARTICULO, l.CANTIDADLINEAENCARGO,a.DESCRIPCIONARTICULO,f.DESCRIPCION,f.IDFAMILIA }
into g
select new { Result = new listaEncargosAgrupados
{
IdArticulo=(int)g.Key.IDARTICULO,
DescripcionArticulo=g.Key.DESCRIPCIONARTICULO,
IdFamilia=g.Key.IDFAMILIA,
DescripcionFamilia=g.Key.DESCRIPCION,
SumaCantidad = (decimal)g.Sum(x => x.l.CANTIDADLINEAENCARGO),
SumaPrecio = (decimal)g.Sum(x => x.l.PRECIOLINEAENCARGO),
Total = (decimal)((decimal)g.Sum(x => x.l.CANTIDADLINEAENCARGO) * g.Sum(x => x.l.PRECIOLINEAENCARGO))
},
IDTIENDAs = new HashSet<int>(from x in g
let id = x.e.IDTIENDA
where id.HasValue
select (int)id.Value)
});
...
if (_finder.IdTienda > 0)
{
query = query.Where(x => x.IDTIENDAs.Contains (_finder.IdTienda));
}
var query1 = query.Select(x => x.Result);
Finally the solution to my problem was to employ executestorequery in the context of my EF, and creat a SQL query :
List<ListaEncargosAgrupados> lista;
string queryString = #"SELECT l.IDARTICULO,a.DESCRIPCIONARTICULO,f.DESCRIPCION descripcionFamilia,f.IDFAMILIA,sum(l.CANTIDADLINEAENCARGO) sumaCantidad,avg(l.PRECIOLINEAENCARGO)
sumaPrecio,sum(l.CANTIDADLINEAENCARGO)*avg(l.PRECIOLINEAENCARGO) Total " +
"FROM lineasencargos l,articulos a,articulofamilia af,familias f,encargos e " +
"where a.IDARTICULO=l.IDARTICULO and a.IDARTICULO=af.IDARTICULO " +
"and af.IDFAMILIA=f.IDFAMILIA and l.IDENCARGO=e.IDENCARGO ";
if (_finder.IdTienda > 0)
{
queryString = queryString + " and e.idtienda=" + _finder.IdTienda;
}
queryString = queryString + " group by l.IDARTICULO,a.DESCRIPCIONARTICULO,f.DESCRIPCION,f.IDFAMILIA order by a.DESCRIPCIONARTICULO ";
var salidaQuery = _contexto.ExecuteStoreQuery<ListaEncargosAgrupados>(queryString).AsQueryable().ToList();
lista = salidaQuery;
basically i want to check DestinationCustomerList with SA_CUSTOMER_ALLOC if any record found based on customerId and fieldId then return priority else priority = 1 , so i am using left outer join with entity but it's not working properly(returning only matching record and if SA_CUSTOMER_ALLOC doesn't have any record throwing "Object referenece error" how to handle this.
var Query = (from p in DestinationCustomerList
join p2 in Context.SA_CUSTOMER_ALLOC on new { f1 = p.CustomerId, f2 = p.FieldId } equals new { f1 = p2.CUSTOMER_ID, f2 = p2.FIELD_ID }
into temp
from x in temp.DefaultIfEmpty()
where x.TAG_ID == TagId && x.REGION_ID == RegionId
select new { p.CustomerId,p.FieldId, Priority = x == null ? 1 : x.PRIORITY }).ToList();
Instead of Priority = x == null ? 1 : x.PRIORITY
try Priority = p2 == null ? 1 : p2.PRIORITY
var Query = (from p in DestinationCustomerList
join p2 in Context.SA_CUSTOMER_ALLOC
on new
{ f1 = p.CustomerId, f2 = p.FieldId }
equals new
{ f1 = p2.CUSTOMER_ID, f2 = p2.FIELD_ID }
into temp
from x in temp.DefaultIfEmpty()
let priority = p2 == null ? 1 : p2.PRIORITY
where x.TAG_ID == TagId && x.REGION_ID == RegionId
select new { p.CustomerId,p.FieldId, Priority = priority }).ToList();
var Player = from PSI in regConfig.Player_SeasonalInfos
from PPI in regConfig.Player_PermanentInfos
where PSI.PaymentId == PlayerPayment.PaymentId
&& PPI.PlayerId == PSI.PlayerId
select new {
PlayerIds = string.Join(",", PSI.PlayerId),
PlayerSeasonalId = PSI.PlayerSeasonalId,
CityId = PPI.CityId
};
foreach (var item in Player)
{
Player_SeasonalInfo PlayerSeasonalInfos =
(from PSI in regConfig.Player_SeasonalInfos
where PSI.PlayerSeasonalId ==item.PlayerSeasonalId
select PSI).FirstOrDefault();
PlayerSeasonalInfos.StatusId = item.CityId == 1 ? 1 : 2;
regConfig.SubmitChanges();
}
i have write this code but i am getting excepiton"invalidcastexception specified cast is not valid linq" on line"
from PSI in regConfig.Player_SeasonalInfos
where PSI.PlayerSeasonalId ==item.PlayerSeasonalId
select PSI"
please suggest.
Hello I need to join two tables (MainTransaction and Subtransaction), the problem here is I also want to get all the record of Maintransaction that's not in the Subtransaction, I am stuck in this part, how can I achieve this ?
protected object SelectMainTbl()
{
var mainIdAndSum = from st in t.subtransaction
group st by st.MainTransactionId into g
select new
{
Sum = (from r in g
select r.Amount).Sum(),
MainId = g.Key
};
var mainTbl = from main in t.maintransaction
join sub in mainIdAndSum on main.MainTransactionId equals sub.MainId
where main.IsEnabled == true && (sub.Sum - main.Amount != 0)
select main;
return mainTbl;
}
I think this is the query that you want:
from mt in t.maintransaction
join st in t.subtransaction
on mt.MainTransactionId equals st.MainTransactionId
into sts
where mt.IsEnabled
where sts.Sum(x => x.Amount) - mt.Amount != 0
select new
{
MainTransaction = mt,
Subtransactions = sts,
};