使用LINQ和ObjectSet查询多个实体时出现问题,输入转换错误?

时间:2013-10-24 17:37:36

标签: entity-framework asp.net-mvc-4 linq-to-entities entity-framework-5

我有一个由多个实体组成的模型。我试图在我的控制器中编写一个LINQ语句来查询其中的一些实体。提前感谢您提供的任何帮助,这让我发疯。

对于每个ObjectSet语句(见下文),我收到以下错误:

Cannot implicitly convert type 'System.Data.Entity.DbSet<MyApp.Models.MY_ENTITY_1>' to 'System.Data.Objects.ObjectSet<MyApp.Models.MY_ENTITY_1>'

这是我的代码:

 public class MyController : ApiController
    {
    private MyEntities context = new MyEntities();

    public IQueryable <string> Get()

    {
        ObjectSet<MY_ENTITY_1> my_entity_1 = context.MY_ENTITY_1; //Error here
        ObjectSet<MY_ENTITY_2> my_entity_2 = context.MY_ENTITY_2; //Error here
        ObjectSet<MY_ENTITY_3> my_entity_3 = context.MY_ENTITY_3; //Error here

        var query =
            from MY_ENTITY_1 in my_entity_1
            from MY_ENTITY_2 in my_entity_2
            from MY_ENTITY_3 in my_entity_3

            where
                 MY_ENITITY_1.something == MY_ENTITY_2.something

            select new
                {
                    Result1 = MY_ENTITY_1.FOO1,
                    Result2 = MY_ENTITY_2.FOO2,
                    Result3 = MY_ENTITY_3.FOO3
                };

        foreach (var myResults in query)
        {
            Console.WriteLine(myResults);
        };

        return myResults;
    }

1 个答案:

答案 0 :(得分:0)

DbSet<T>不会继承ObjectSet<T>

您可以通过

进行更改
IQueryable<MY_ENTITY_1> my_entity_1 = context.MY_ENTITY_1;
IQueryable<MY_ENTITY_2> my_entity_2 = context.MY_ENTITY_2;
IQueryable<MY_ENTITY_3> my_entity_3 = context.MY_ENTITY_3;

或者只是从该查询中删除该中间对象

var query = from MY_ENTITY_1 in context.MY_ENTITY_1
            from MY_ENTITY_2 in context.MY_ENTITY_2
            from MY_ENTITY_3 in context.MY_ENTITY_3
            where MY_ENITITY_1.something == MY_ENTITY_2.something
            select new
            {
                Result1 = MY_ENTITY_1.FOO1,
                Result2 = MY_ENTITY_2.FOO2,
                Result3 = MY_ENTITY_3.FOO3
            };