How to use dynamic keywork to work with entity framework
I am using .net framework 3.5 framework & for database operations entity framework. So, to fetch the data i am using LINQ queries & returns a resultset in a list. e.g
public class Emp
{
public int CD{get;set;}
public string Name{get;set;}
}
public List<Emp> GetServTypeForPromotionDue()
{
return (from a in Context.TableName
select new Emp{ a.CD, a.NAME });
}
I am planning to migrate from 3.5 to 4.0. I have heard about dynami开发者_运维百科c keyword in .net 4.0. Can i make use of 'dynamic' keyword in order to remove the Class Emp & without creating Anonymous methods?
You could use dynamic
for this (e.g. returning List<dynamic>
, and building an ExpandoObject
in the select clause), but I wouldn't recommend it. You'd lose compile-time type safety, Intellisense etc for little benefit. What do you believe you'd gain from using dynamic
? Just removing a few lines of code?
EDIT: Example of using dynamic:
public List<dynamic> GetServTypeForPromotionDue()
{
return (from a in Context.TableName
select (dynamic) (new { a.CD, a.NAME })).ToList();
}
Or...
public List<dynamic> GetServTypeForPromotionDue()
{
return (from a in Context.TableName
select (dynamic) (new ExpandoObject { a.CD, a.NAME })).ToList();
}
精彩评论