I have the following query in controller and I want to store a column value in a variable but I am not being able to iterate it. Here is my code:
var srmas = (
from SRMAs in db.SRMAs
join SRMAStatus in db.SRMAStatus on SRMAs.Status equals SRMAStatus.Id
join PurchaseOrders in db.PurchaseOrders on SRMAs.PONumber equals PurchaseOrders.PONumber
join Suppliers in db.Suppliers on PurchaseOrders.SupplierID equals Suppliers.SupplierID
join SRMADetails in db.SRMADetails on SRMAs.Id equals SRMADetails.SRMAId
where(SRMAs.Id == srmaid)
group SRMADetails by new
{
SRMADetails.Id,
SRMADetails.SRMAId,
SRMADetails.SupplierPartNum,
SRMAs.PONumber,
SRMAs.ActualAmount,
SRMAs.ApprovedOn,
SRMAs.Status,
SRMAs.TrackingNumber,
SRMAs.SupplierRMANumber,
SRMAs.RequestedFromSupp,
SRMAs.CreatedOn,
Suppliers.SupplierName,
SRMAStatus.StatusName,
PurchaseOrders.PODate,
PurchaseOrders.suppliersOrderNumber
} into grp
select new
{
grp.Key.Status,
grp.Key.SRMAId,
grp.Key.Id,
grp.Key.PONumber,
grp.Key.SupplierRMANumber,
grp.Key.ActualAmount,
grp.Key.SupplierPartNum,
grp.Key.RequestedFromSupp,
grp.Key.TrackingNumber,
grp.Key.ApprovedOn,
grp.Key.SupplierName,
grp.Key.StatusName,
grp.Key.PODate,
grp.Key.suppliersOrderNumber,
grp.Key.CreatedOn,
Sum = grp.Sum(SRMADetails => SRMADetails.Cost * SRMADetails.QtyReturned)
}
).ToList();
System.Collections.IEnumerable et = (System.Collections.IEnumerable)srmas;
IEnumerator it = et.GetEnumerator();
while (it.MoveNext())
{
SRMA current = (SRMA)it.Current;
Response.Write(current.Status);
}
ViewBag.SRMAs = srmas.Select(srma => new IndexViewModel
{
Id = srma.SRMAId,
SupplierRMANum = srma.SupplierRMANumber,
SRMADetailsID = srma.Id,
PONumber = srma.PONumber,
CreatedOn = srma.CreatedOn,
SupplierName = srma.SupplierName,
SRMAStatus = srma.StatusName,
Status = srma.Status,
suppliersOrderNumber = srma.suppliersOrderNumber,
PODate = srma.PODate,
Sum = srma.Sum,
TrackingNumber = srma.TrackingNumber,
ActualAmount = srma.ActualAmount
}).ToList();
I just want to get Status value of first record. How do I do it?
Related
I need an help here! I always get an error from linq (wrong expression). Should work but it´s not... I don´t get any error when order by max Id but I need to get the Id of the max date group by IDFiscal and CodigoXPCliente. I tried diferent ways but I always get an error.
var queryTemp =
(from u in _userManager.Users.Where(u => u.TipoUsuario == "Cliente")
join ass in context.AssessoresCliente on u.Id equals ass.ApplicationUser.Id into temp_ass
from temp in temp_ass
select new ListaBaseCliente_Atendimento
{
Nome = u.Nome,
IDFiscal = u.IDFiscal,
Email = u.Email,
PhoneNumber = u.PhoneNumber,
DataPessoa = u.DataPessoa,
DataCadastro = temp.DataCadastro,
CodigoXPCliente = temp.CodigoXPCliente,
AssessorCambio = temp.AssessorCambio,
AssessorProtect = temp.AssessorProtect,
CodigoAssessorXP = temp.CodigoAssessorXP,
ClienteCambio = temp.ClienteCambio,
ClienteProtect = temp.ClienteProtect,
ClienteXP = temp.ClienteXP,
AssessorXP = temp.AssessorXP,
CadastroValido = true,
DataRegistroUsuario = temp.DataRegistroUsuario,
DataArquivo = DataHoje,
ContaXPNova = temp.ContaXPNova,
ImportacaoBitrix = temp.ImportacaoBitrix,
IdAssessoresCliente = temp.Id,
RegistroValido = temp.RegistroValido
}).AsEnumerable();
var agrup = from s in queryTemp group s by new { s.IDFiscal, s.CodigoXPCliente } into g
select g.OrderByDescending(t => t.DataRegistroUsuario).FirstOrDefault();
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();
I have the following Linq expression:
var employeeTypes = from t in DbContext.Set<SetupEmployeeType>().AsNoTracking()
join emp in DbContext.Set<Employee>().AsNoTracking() on t.EmployeeTypeId equals emp.EmployeeTypeId into employee
from subemp in employee.DefaultIfEmpty()
where t.MasterEntity == masterEntity
select new Model.SetupEmployeeTypeModel()
{
EmployeeTypeId = t.EmployeeTypeId,
Description = t.Description,
AllowProbation = t.AllowProbation,
IsActive = t.IsActive,
TotalEmployee = (subemp == null ? 0 : subemp.Count)
};
I need to set TotalEmployee property of my custom model.
So if there is no EmployeeTypeId associated to any Employee then TotalEmployee should be 0, else should be the Count of Employees.
Any clue how to do this?
If i may not wrong then consider using a subquery, like so -
var q = from empType in DbContext.Set<SetupEmployeeType>().AsNoTracking()
let empCount =
(
from emp in DbContext.Set<Employee>().AsNoTracking()
where empType.EmployeeTypeId == emp.EmployeeTypeId
select emp
).Count()
select new Model.SetupEmployeeTypeModel()
{
EmployeeTypeId = empType.EmployeeTypeId,
Description = empType.Description,
AllowProbation = empType.AllowProbation,
IsActive = empType.IsActive,
TotalEmployee = empCount
};
Using Group by.
var q = from empType in DbContext.Set<SetupEmployeeType>().AsNoTracking()
join empCnt in
(
from emp in DbContext.Set<Employee>().AsNoTracking()
group emp by emp.EmployeeTypeId into grp
select new { EmployeeTypeId = grp.Key, TotalEmp = grp.Count()}
) on empType.EmployeeTypeId equals empCnt.EmployeeTypeId into employees
from subemp in employees.DefaultIfEmpty()
where t.MasterEntity == masterEntity
select new Model.SetupEmployeeTypeModel()
{
EmployeeTypeId = empType.EmployeeTypeId,
Description = empType.Description,
AllowProbation = empType.AllowProbation,
IsActive = empType.IsActive,
TotalEmployee = subemp.TotalEmp
};
I came up with the following solution:
var employeeTypes = from t in DbContext.Set<SetupEmployeeType>().AsNoTracking()
join empg in
(
from emp in DbContext.Set<Employee>().AsNoTracking()
group emp by emp.EmployeeTypeId into g
select new { EmployeeTypeId = g.Key, Total = g.Count() }
) on t.EmployeeTypeId equals empg.EmployeeTypeId into employee
from subemp in employee.DefaultIfEmpty()
where t.MasterEntity == masterEntity
select new Model.SetupEmployeeTypeModel()
{
EmployeeTypeId = t.EmployeeTypeId,
Description = t.Description,
AllowProbation = t.AllowProbation,
IsActive = t.IsActive,
TotalEmployee = subemp.Total
};
I am doing following and it works:
var resultsHeader = (from r in o select new {
OrderID = r.OrderID,
subtotal = r.SubTotal,
city = r.orderAddress.BilltoCity
}).Distinct().ToList();
Look at the city = r.orderAddress.BilltoCity Line, is there any way i can populate the entire object there...like this
var resultsHeader = (from r in o select new {
OrderID = r.OrderID,
subtotal = r.SubTotal,
Address = r.orderAddress
}).Distinct().ToList();
Basicly, I want to store entire OrderAddress Object into Address property, however it does not come out as Distinct object.
You can either use DistinctBy from moreLINQ:
var resultsHeader = (from r in o select new {
OrderID = r.OrderID,
subtotal = r.SubTotal,
city = r.orderAddress.BilltoCity
}).DistinctBy(x => new { x.OrderID, x.subtotal }).ToList();
or GroupBy, which is part of LINQ already:
var resultsHeader = (from r in o
group r by new { r.OrderIs, r.SubTotal } into g
select new {
g.Key.OrderID,
g.Key.SubTotal,
Address = g.First().orderAddress
}).ToList()
UPDATE
Or, you can implement Equals and GetHashCode on your Address class and use Distinct as you do now.