Select All Rows Using Entity Framework

前端 未结 8 511
无人共我
无人共我 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:37

    You can simply iterate through the DbSet context.tablename

    foreach(var row in context.tablename)
      Console.WriteLn(row.field);
    

    or to evaluate immediately into your own list

    var allRows = context.tablename.ToList();
    
    0 讨论(0)
  • 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"))
    
        }
    
    0 讨论(0)
提交回复
热议问题