休眠“加入”
有什么办法可以优化这个解决方案吗? 是否有可能通过调用单个查询获得相同的结果?
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;
}
我的流畅映射:
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");
}
}
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");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,这是更好的查询
,但有一个问题,
而不是给出通过 StockQuote.Date 从 Company.StockQuotes 中选择 StockQuote 的时间段,想要真正在这个连接的集合上创建 TOP()
,我只知道 maxDate (在本例中),计算 minDate 非常困难
ok, it is better query
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