休眠“加入”

发布于 2024-09-15 20:01:51 字数 3511 浏览 5 评论 0原文

有什么办法可以优化这个解决方案吗? 是否有可能通过调用单个查询获得相同的结果?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎夜雨 2024-09-22 20:01:52

好吧,这是更好的查询

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();
        }

,但有一个问题,
而不是给出通过 StockQuote.Date 从 Company.StockQuotes 中选择 StockQuote 的时间段,想要真正在这个连接的集合上创建 TOP()

,我只知道 maxDate (在本例中),计算 minDate 非常困难

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文