1

我有一个由多个实体组成的模型。我正在尝试在我的控制器中编写一个 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;
    }
4

1 回答 1

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
            };
于 2013-10-24T18:05:20.887 回答