LINQ到实体,而不是存储过程? [英] LINQ to Entities instead of stored procedure?

查看:127
本文介绍了LINQ到实体,而不是存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架来访问我的数据库,我只是想通了,映射的存储过程使用实体的方法是有点太复杂,可能不太适合做我想做exacltly。我是新来的LINQ和我想知道如果我是前人的精力更好只是ADO.NET。下面是SQL代码,我需要翻译:

I am using the Entity Framework to access my database and I just figured out that mapping stored procedures to methods using entity is a bit too complex and probably not well suited to do what I want exacltly. I am new to LINQ and I am wondering if I sould be better off just with ADO.NET. Here is the SQL code I need to translate:

SELECT p.Player_id, p.Name, p.Position, SUM(s.Goals) AS goalsb, SUM(s.Assists) AS assistsb, SUM(s.Points) AS pointsb
FROM Dim_Player AS p INNER JOIN Fact_Statistics AS s ON s.Player_id = p.Player_id
GROUP BY p.Player_id, p.Name, p.Position
ORDER BY pointsb DESC, goalsb DESC

随着实体映射存储过程,我有这个代码写的:

With the Entities mapping for stored procedures, I had this code written:

HockeyStatsEntities db = new HockeyStatsEntities();

public ActionResult Index()
{
    ViewBag.Message = "League leaders";
    {
        return View(db.ListLeagueLeaders());
    }
}

public ActionResult About()
{
    return View();
}

private ICollection<ListLeagueLeaders_Result> ListLeagueLeaders()
{
    ICollection<ListLeagueLeaders_Result> leagueLeadersCollection = null;

    using (HockeyStatsEntities context = new HockeyStatsEntities())
    {
        foreach (ListLeagueLeaders_Result leagueLeader in
                  context.ListLeagueLeaders())
        {
            leagueLeadersCollection.Add(leagueLeader);
        }
    }
    return leagueLeadersCollection;
}

下面是我得到的错误:

传递到字典中的模型产品类型
的'System.Data.Objects.ObjectResult`1 [NHLStats2.Models.ListLeagueLeaders_Result]',
,而这本词典需要输入
'NHLStats2.Models.ListLeagueLeaders_Result的典范项目。

The model item passed into the dictionary is of type 'System.Data.Objects.ObjectResult`1[NHLStats2.Models.ListLeagueLeaders_Result]', but this dictionary requires a model item of type 'NHLStats2.Models.ListLeagueLeaders_Result'.

但我知道这有一点在和放大器痛; * @ ......我怎么会用一种不同的,更有效的方法重新安排呢? 。感谢您的帮助,这真是感激

But I realize that this is a bit of a pain in the ?&*@... How could I re-arrange this using a different, more effective method? Thanks for your help, it's really appreciated.

推荐答案

最后选择了使用存储过程,这个编译和作品:

Finally opting for using a stored procedure, this compiles and works:

Model1.tt.Context.cs (自动生成)

public DbSet<Dim_Date> Dim_Date { get; set; }
    public DbSet<Dim_Player> Dim_Player { get; set; }
    public DbSet<Dim_Team> Dim_Team { get; set; }
    public DbSet<Fact_Statistics> Fact_Statistics { get; set; }
    public DbSet<Dim_Game> Dim_Game { get; set; }

    public virtual ObjectResult<ListLeagueLeaders_Result> ListLeagueLeaders()
    {
        ((IObjectContextAdapter)this).ObjectContext.MetadataWorkspace.LoadFromAssembly(typeof(ListLeagueLeaders_Result).Assembly);

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<ListLeagueLeaders_Result>("ListLeagueLeaders");
    }

查看

@model System.Data.Objects.ObjectResult<NHLStats2.Models.ListLeagueLeaders_Result>

@{
    ViewBag.Title = "Index";
}

[...]

问题是与强类型的视图类型不匹配的System.Data.Objects.ObjectResult类型。是: @model NHLStats2.Models.ListLeagueLeaders_Result ,更改为: @model System.Data.Objects.ObjectResult< NHLStats2.Models.ListLeagueLeaders_Result> 干杯!

Problem was with the strongly typed view type not matching the System.Data.Objects.ObjectResult type. Was: @model NHLStats2.Models.ListLeagueLeaders_Result, changed to: @model System.Data.Objects.ObjectResult<NHLStats2.Models.ListLeagueLeaders_Result> Cheers!

这篇关于LINQ到实体,而不是存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆