LINQ wrapper class around binary DataType
I have a LINQ to SQL class called Data with a column of type Data.Linq.Binary. I want to create a wrapper class that extracts the actual object from the Binary field. I have multiple classes that store information in the Data table. I know by context, as in each class that stores information in the Data table ALWAYS stores the same type. I have a class Something
with a method that adds/reads a string
to/from the Data table.
class Data // LINQ to SQL generated
{
System.Data.Linq.Binary Value {get; set;}
string Name {get; set;}
int ID {get; set;}
}
class Something
{
void Add(string s)
{
//using (db)
Data.Value = s.ToBinary(); //Convert the string to a byte[] then to Binary
}
}
I then want a property that reads from the Binary column:
class Something
{
string Value
{
//using (db)
get{ return Data.Value.ToString();//Convert to byte[] then to string }
}
}
This by itself works perfectl开发者_开发问答y fine, but I have multiple classes that do this same interaction. Naturally I want an interface or an abstract class, something like this:
interface Binary<T>
{
void Add(T t);
T Value {get;}
}
Here is my problem: In the Value getter I have a linq query that actually returns the LinqToSql Data class with a Linq.Binary Value
, but in my interface I have a T Value
. How do I cast a linq query to this Binary interface? Something like this, although it doesn't work:
List<Binary<T>> Value
{
get
{
return (from D in db.Data
select D).Cast<Binary<T>>();
}
}
Edit: I had the wrong Property at the very end. It should be Value, not Name.
Unless you want your Data
class to be generic itself, it has to implement Binary<T>
for a particular T
, i.e. string
, so the class declaration looks like this:
class MyDataWrapper : Binary<string>
{
...
In that case the Name implementation looks something like this:
List<Binary<string>> Name
{
get
{
return (from D in db.Data
select D).Cast<Binary<string>>();
}
}
精彩评论