Simple way to return anonymous types (to make MVC using LINQ possible)

后端 未结 6 1959
囚心锁ツ
囚心锁ツ 2021-01-05 19:03

I\'d like to implement MVC while using LINQ (specifically, LINQ-to-entities). The way I would do this is have the Controller generate (or call something which generates) th

6条回答
  •  星月不相逢
    2021-01-05 19:19

    How about this?

    I assume that you have an entity class for your table 'myTable' (let's call it 'MyTableEntity'), so why don't you instantiate a new MyTableEntity object and use object initializer to fill only those columns you want?

    return (from o in myTable select new MyTableEntity { AColumn = o.column });
    

    This will not translate to a SELECT * as you asked, but you'll still have a way to pass a strongly-typed object to a view.

    You have to be careful to just make use of the initialized properties inside the view and that's it.

    Does this makes sense for you?

提交回复
热议问题