Select All Rows Using Entity Framework

前端 未结 8 608
无人共我
无人共我 2021-02-01 00:48

I\'m trying to select all the rows out of a database using entity framework for manipulation before they\'re sent to the form

var ptx = [modelname].[tablename]()         


        
8条回答
  •  不要未来只要你来
    2021-02-01 01:41

    Here is a few ways to do it (Just assume I'm using Dependency Injection for the DbConext)

    public class Example
    {
        private readonly DbContext Context;
    
        public Example(DbContext context)
        {
            Context = context;
        }
    
        public DbSetSampleOne[] DbSamples { get; set; }
    
        public void ExampleMethod DoSomething()
        {
            // Example 1: This will select everything from the entity you want to select
            DbSamples = Context.DbSetSampleOne.ToArray();
    
            // Example 2: If you want to apply some filtering use the following example
            DbSamples = Context.DbSetSampleOne.ToArray().Where(p => p.Field.Equals("some filter"))
    
        }
    

提交回复
热议问题