Select All Rows Using Entity Framework

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

    How about:

    using (ModelName context = new ModelName())
    {
        var ptx = (from r in context.TableName select r);
    }
    

    ModelName is the class auto-generated by the designer, which inherits from ObjectContext.

    0 讨论(0)
  • 2021-02-01 01:27

    You can use this code to select all rows :

    C# :

    var allStudents = [modelname].[tablename].Select(x => x).ToList();
    
    0 讨论(0)
  • 2021-02-01 01:27

    Old post I know, but using Select(x => x) can be useful to split the EF Core (or even just Linq) expression up into a query builder.

    This is handy for adding dynamic conditions.

    For example:

    public async Task<User> GetUser(Guid userId, string userGroup, bool noTracking = false)
    {
        IQueryable<User> queryable = _context.Users.Select(x => x);
    
        if(!string.IsNullOrEmpty(userGroup))
            queryable = queryable.Where(x => x.UserGroup == userGroup);
    
        if(noTracking)
            queryable = queryable.AsNoTracking();
    
        return await queryable.FirstOrDefaultAsync(x => x.userId == userId);
    }
    
    0 讨论(0)
  • 2021-02-01 01:28

    You can use:

    ptx.[tablename].Select( o => true)
    
    0 讨论(0)
  • 2021-02-01 01:29

    I used the entitydatasource and it provide everything I needed for what I wanted to do.

    _repository.[tablename].ToList();

    0 讨论(0)
  • 2021-02-01 01:35

    Entity Framework has one beautiful thing for it, like :

    var users = context.Users; 
    

    This will select all rows in Table User, then you can use your .ToList() etc.


    For newbies to Entity Framework, it is like :

    PortalEntities context = new PortalEntities();
    var users = context.Users;
    

    This will select all rows in Table User

    0 讨论(0)
提交回复
热议问题