foreach (lc_ShoppingCart sc in shQuery)
{
//Decrement the Product Table's Total Remaining row with the quantity
var ProductInventoryQuery = (from pr in db.lc_ProductInventories
join c in db.lc_ColorTables on pr.Color equals c.Color
join s in db.lc_SizeTables on pr.Size equals s.Size
where pr.ProductID == Convert.ToInt32(sc.ProductID)
where pr.Color == c.Color
where pr.Size == s.Size
select pr).First();
ProductInventoryQuery.Quantity = ProductInventoryQuery.Quantity - sc.Quantity;
}
Probably something like this:
var ProductInventoryQuery =
db.lc_ProductInventories.Where(w => w.ProductID == Convert.ToInt32(sc.ProductID))
.Join(db.lc_ColorTables, p => p.Color, ct => ct.Color, (p, ct) => new { ProdInv = p, ColorTables = ct })
.Join(db.lc_SizeTables, p => p.Size, st => st.Color, (p, st) => new { ProdInv = p, SizeTables = st })
.First();
Related
I have this situation:
I have a form in ASP.NET and I need to extract data from a mssql db. The LINQ query is build from the values that are inserted in the form.
if (ddlRegion.SelectedIndex > 0)
{
query = query.Where(re => re.Region == ddlRegion.SelectedValue);
}
if (tbName.Text.Trim().Length > 0)
{
query = query.Where(na => na.Name.Contains(tbName.Text));
}
var result = query.Select(res => new
{
res.ColumnA,
res.ColumnB,
res.ColumnC
});
The problem is that I need to make a join with TableB
query = query.Join(TableB, tA => tA.Code, tB => tB.CodFiscal, (tA, tB) => tA);
The original SQL command is like this:
select tA.ColumnA, tA.ColumnB, tA.ColumnC from TableA tA join TableB tB on tA.Code=tB.Code where tB.ExpireDate>=getdate() and tB.datavalabil >=getdate()
The problem is where clauses from table tB join.
You can do something like this:
query = query.Join(TableB, tA => tA.Code, tB => tB.CodFiscal, (tA, tB) => new { tA, tB })
.Where(x => x.tB.ExpireDate >= DateTime.Now and x.tB.datavalabil >= DateTime.Now)
.Select(x => x.tA);
Or in query syntax:
query =
from tA in query
join tB in TableB on tA.Code equals tB.CodFiscal
where tB.ExpireDate >= DateTime.Now and tB.datavalabil >= DateTime.Now
select tA;
I have the below query that gets Name and TotalPoints as follows:
var gradeData = (from data in oAngieCtxt.prc_ShopInstanceCustomersData(Convert.ToInt32(this.ShopInstanceID), 10000, false)
.Where(row => row.RecievedPoints != "n/a")
.GroupBy(row => new { row.Name })
.Select(g => new
{
TotalPoints = g.Sum(x => Convert.ToDouble(x.RecievedPoints) * (x.Weightage.ToString() == "0.00" ? 1 : Convert.ToDouble(x.Weightage))),
Name = g.Key.Name
})
select data).ToList();
I will have data like below:
TotalPoints Name
5 A
10 B
5 C
15 D
5 E
If we observe the above list 5 is most common. I have to fetch that value from "gradeData".
How can I get that?
var mostCommon = gradeData.GroupBy(x => x.TotalPoints)
.OrderByDescending(g => g.Count())
.Select(g => g.Key)
.First();
The query below gives you the most common point along with all of its associated names:
var result = gradeData.GroupBy(x => x.TotalPoints)
.OrderByDescending(x => x.Count())
.Select(g => new
{
TotalPoints = g.Key,
Names = g.Select(x => x.Name).ToList()
})
.First();
If I have a lystId, I want to include the MemberProductLyst object and filter by the LystId.
Any suggestions for the proper way to implement the follow up Lamba code inside of the
if (!string.IsNullOrEmpty(lystId)) {} block below the initial query???
products = (from p in dc.Products
join t in dc.Tags on p.TagId equals t.TagId
join pi in dc.ProductImages on p.ProductId equals pi.ProductId
join i in dc.Images on pi.ImageId equals i.ImageId
where p.IsActive == true
where t.TagId == new Guid(brandId)
orderby p.CreatedOn descending
select new ProductItem
{
productImage = i.FileName,
productId = p.ProductId,
description = p.Description,
name = p.Name,
adImage = t.AdImage
}).Skip(totalItemCount).Take(pageSize);
if (!string.IsNullOrEmpty(lystId))
{
//Include MemberProductLyst table to Filter by lystId if LystId is available
var memberLysts = from mpl in dc.MemberProductLysts
where mpl.LystId == new Guid(lystId)
select new { mpl.LystId, mpl.ProductId };
products = (IQueryable<ProductItem>)products.Join(memberLysts, p => p.productId, mpl => mpl.ProductId, (p, mpl) => new {ProductItem = p, MemberProductLyst = mpl });
}
It largely depends on the intent of your Join, but I suspect this may yield the results you're looking for:
products = products.Where(
p => memberLysts.Any(mpl => mpl.ProductId == p.productId));
i want Replace بازي to بازی
var List = (from darkhast in Tbl_Darkhast.Where(d => d.Address.Replace("ی","ي").StartsWith( Address.Replace("ی","ي") ) )
select new
{
....
}
I think,You should write in select query
var List = (from darkhast in Tbl_Darkhast) )
select new Tbl_Darkhast
{
Address= darkhast.Address.Replace("ی","ي")
,Name = darkhast.Name
};
The distance and character displacement:
var List = (from darkhast in Tbl_Darkhast.Where(d => d.Address.Replace(" ", "").Replace("ي", "ی").Replace("ك", "ک").StartsWith(Address.Replace(" ", "").Replace("ي", "ی").Replace("ك", "ک")))
select new
{
....strong text
}
result is =>
var List = (from darkhast in Tbl_Darkhast.Where(d => d.Name_Moteghazi.Replace(" ", "").Replace("ي", "ی").Replace("ك", "ک").StartsWith(NameMoteghazi.Replace(" ", "").Replace("ي", "ی").Replace("ك", "ک")))
select new
{
Shomare_Darkhast = darkhast.Shomare_Darkhast,
Tarikhe_Darkhast = darkhast.Tarikhe_Darkhast,
Sharhe_NoeDarkhast = darkhast.TBL_NoeDarkhast.Sharhe_NoeDarkhast,
Mantaghe = darkhast.Mantaghe,
Hoze = darkhast.Hoze,
Block = darkhast.Block,
Melk = darkhast.Melk,
});
return (ObjectQuery)List;
is any way to optimize this solution?
whether there is any possibility of obtaining the same result by calling single query?
public List<Company> GetCompanies(DateTime maxDate, int stockQuotesCount)
{
List<Company> result = new List<Company>();
IList<Company> company = null;
DateTime lastSessionDate = new StockQuoteRepository().GetLastSessionDate(maxDate);
using (ISession s = DataAccessFacade.OpenSesion())
{
String sqlQuery = string.Empty;
sqlQuery = #"Select
*
From
dbo.Company c
Where
c.Company_FirstQuotationDate <= :date and
(c.Company_LastQuotationDate >= :date or c.Company_LastQuotationDate is Null)
Order By
c.Company_Name asc";
company = s.CreateSQLQuery(sqlQuery)
.AddEntity(typeof(Company))
.SetDateTime("date", lastSessionDate)
.List<Company>();
if (company != null)
{
for (int i = 0; i < company.Count; i++)
{
sqlQuery = #"Select
Top(:top)
*
From
dbo.StockQuote sq
Where
sq.StockQuote_Company_Id = :cId
and sq.StockQuote_Date <= :date
Order By
sq.StockQuote_Date desc";
company[i].StockQuotes = s.CreateSQLQuery(sqlQuery)
.AddEntity(typeof(StockQuote))
.SetParameter<int>("cId", company[i].Id)
.SetParameter<int>("top", stockQuotesCount)
.SetDateTime("date", lastSessionDate)
.List<StockQuote>();
}
}
}
return (List<Company>)company;
}
my fluent mapings:
public class CompanyMap : ClassMap<Company>
{
public CompanyMap()
{
this.Id(x => x.Id).Column("Company_Id");
this.Map(x => x.Name).Column("Company_Name");
this.Map(x => x.FirstQuotationDate).Column("Company_FirstQuotationDate");
this.Map(x => x.LastQuotationDate).Column("Company_LastQuotationDate");
this.HasMany(x => x.StockQuotes)
.Cascade.All()
.BatchSize(50)
.Inverse();
}
}
public class StockQuoteMap : ClassMap<StockQuote>
{
public StockQuoteMap()
{
this.Id(x => x.Id).Column("StockQuote_Id");
this.Map(x => x.Open).Column("StockQuote_Open");
this.Map(x => x.Low).Column("StockQuote_Low");
this.Map(x => x.High).Column("StockQuote_High");
this.Map(x => x.Close).Column("StockQuote_Close");
this.Map(x => x.Volume).Column("StockQuote_Volume");
this.Map(x => x.Date).Column("StockQuote_Date");
this.References(x => x.Company).Column("Company_Id");
}
}
ok, it is better query
using (ISession s = DataAccessFacade.OpenSesion())
{
String cHql = #"select
distinct c
from
Company c
join fetch c.StockQuotes s
where
c.FirstQuotationDate <= :maxDate and
(c.LastQuotationDate >= :maxDate or c.LastQuotationDate is Null)
and s.Date >= :minDate
order by
c.Name asc,
s.Date desc";
result = s.CreateQuery(cHql)
.SetDateTime("maxDate", lastSessionDate)
.SetDateTime("minDate", lastSessionDate.AddMonths(-2))
.List<Company>().ToList();
}
but there is one problem,
instead of giving time period for selecting StockQuote by StockQuote.Date form Company.StockQuotes, wants to make the TOP() on this joined collection
really, I only know maxDate (in this case), the calculation minDate is very difficult