c# - Retrieving one object by using GetTable method over DataContext -


I have inherited a class from Datacontext for using Linux. Public class context: Datacontext {public context (string connection string): base (connection string) {}} [table (name = "tablename")] Public class ClassOfTable {}

And I have another class that has been mapped on a table to get all the lines in the class

  context.getteable  

method, which is mapped to classfotable classes. But I want to get just one row from the database table. I can use it this way:

  classoffable cls = context.getetable & lt; ClassOfTable & gt; (). Where (P = & gt; p.id == 1). first ();  

But it will retrieve every row of the table and I do not want to do this. What should I do to take only one row from the table?

This will not retrieve all the rows of a table, this is just 1 row via a statement in SQL Can get from Remember that the link is, GetTable & lt; T & gt; actually runs anything, only when . The first () runs is called anything.

We often add this method to our datacentext as often as this extension:

  Public Stable GetById & lt; T & gt; (This DataContext DC, Long ID) where T: Class, IBaseEntity {Return DeggetTable & lt; T & gt; (). Single (T = & gt; TID == ID); }  

Our interface, which is in every category, can be very small for this purpose:

  Public Interface IBaseEntity {long Id {get; Set; }}  

Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -