开发者

Assigning Linq query to object type

I'm learning Linq to Sql;

Is it always the case that you simply pass the query directly as an assignment to the business object type, as done here in the "return line" ? What are the limitations of this? What are the requirements; That the property name must be equal to the database column name?

What if:

  1. The business object have other properties in addition to those saved in the database
  2. The business object is missing one property to match with a column name?

Any other relevant info on limitations and things to watch out for would be useful.

Example:

public List<Beer> GetAllBeers()
{
    DataClassesDataContext db = new DataClassesDataContext();
    var beers = 开发者_StackOverflowfrom b in db.Beers
                orderby b.BeerName
                select b;
    return beers.ToList<Beer>();
}


When you created your dbml file, and dragged your Beer table on the canvas, the designer generates a class called Beer for you. By default this class maps 1-1 to the table - a property for each column, same datatypes etc.

You can change those properties if you want to - you can change their name and datatype, you can remove them (they will need to be nullable in the database if you remove them), you can add properties that don't necessarily map to the database, and so on.

When you instantiate a data context object, you can retrieve those Beer objects (the class, not the table) from the database using db.Beers. LINQ-to-SQL populates these objects according to the rules laid out in the designer - i.e. which column maps to which property, the data types etc. Any property on the class which doesn't map to a column will be given default value, just like a normal initialised object.

You can use those objects just as you would use any other object; they are just classes with additional attributes. You can see them in the MyDatabase.designer.cs file which sits behind the dbml file. Put them in a list and return them, project them into other objects, select just their id properties - whatever you need to do.


No its not always the case...however its very common as when you configure your app for linq to sql you do end up with a mapping of your domain and data models.

you can however use projection to shape your results into whatever class or value type you want...

DataClassesDataContext db = new DataClassesDataContext();
var suppliers = from b in db.Beers
                orderby b.BeerName
                select b.Supplier;

This would return a IEnumerable<string> of just the supplier property.

Or using either an anonymous class or even your own class you can project to that as well...

var suppliers = from b in db.Beers
                orderby b.BeerName
                select new ()
                {
                  Supplier = b.Supplier,
                  BeerName = b.BeerName
                };

or your own class...

var suppliers = from b in db.Beers
                orderby b.BeerName
                select new MyClass()
                {
                  Supplier = b.Supplier,
                  BeerName = b.BeerName
                };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜