Why is ORM considered good but “select *” considered bad?

后端 未结 12 1694
误落风尘
误落风尘 2021-01-29 23:25

Doesn\'t an ORM usually involve doing something like a select *?

If I have a table, MyThing, with column A, B, C, D, etc, then there typically would be an object, MyThin

12条回答
  •  长情又很酷
    2021-01-30 00:16

    Linq to Sql, or any implementation of IQueryable, uses a syntax which ultimately puts you in control of the selected data. The definition of a query is also the definition of its result set.

    This neatly avoids the select * issue by removing data shape responsibilities from the ORM.

    For example, to select all columns:

    from c in data.Customers
    select c
    

    To select a subset:

    from c in data.Customers
    select new
    {
      c.FirstName,
      c.LastName,
      c.Email
    }
    

    To select a combination:

    from c in data.Customers
    join o in data.Orders on c.CustomerId equals o.CustomerId
    select new
    {
      Name = c.FirstName + " " + c.LastName,
      Email = c.Email,
      Date = o.DateSubmitted
    }
    

提交回复
热议问题