I’m using LINQ to SQL and I’m wondering if there is a short cut to this syntax:
using (Model db = new Model())
{
LinqObj l = db.LinqObj.Where(o => o.ID = id).Single();
}
I know I could just create a static method that returns a LinqObj object:
public static LinqObj FromID(int id)
{
using (Model db = new Model())
{
return LinqObj l = db.LinqObj.Where(o => o.ID = id).Single();
}
}
Which got me thinking; is there a way to create a constructor that returns the object:
LinqObj l = new LinqObj(ID: 7);
What is the best way to create an object of a single row in a LINQ to SQL object?
You can have a utility method for the LINQ to SQL boilerplate:
And then just do this: