
Untitled
By: a guest on
Apr 15th, 2012 | syntax:
None | size: 1.62 KB | hits: 8 | expires: Never
Assign an object to the class from within
public partial class Table1
{
public Table1(int id)
{
using (DBDataContext item = new DBDataContext())
{
var q = (from c in item.table1
where c.ID == id
select c).FirstOrDefault();
}
}
}
public static Table1 TableFromId(int Id)
{
using (DBDataContext item = new DBDataContext())
{
return (from c in item.table1
where c.ID == id
select c).FirstOrDefault() as Table1;
}
}
Table1 table = Table1.TableFromId(0);
public class Person: ICopyable<Person>
{
public string Name {set; get;}
public int Age {set; get;}
public void CopyFrom(Person otherObject)
{
if (otherObject != null)
{
Name = otherObject.Name;
Age = otherObject.Age;
}
}
public Table1(int id)
{
using (DBDataContext item = new DBDataContext())
{
var q = (from c in item.table1
where c.ID == id
select c).FirstOrDefault() as Person;
if (q != null)
{
CopyFrom(q);
}
}
}
}
public interface ICopyable<TType>
{
void CopyFrom(TType otherObject);
}
public partial class Table1
{
public static FromID(int id)
{
using (var context = new DBDataContext()) {
return (Table1)(from c in context.table1
where c.ID == id
select c).FirstOrDefault();
}
}
}
Table1 obj = Table1.FromID(17);