围绕二进制数据类型的 LINQ 包装类
我有一个名为 Data 的 LINQ to SQL 类,其中有一列类型为 Data.Linq.Binary。我想创建一个包装类,从 Binary 字段中提取实际对象。我有多个类在数据表中存储信息。我通过上下文知道,因为在数据表中存储信息的每个类中始终存储相同的类型。我有一个类 Something
,其方法可以向数据表添加/读取 string
。
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
}
}
然后,我想要一个从 Binary 列读取的属性:
class Something
{
string Value
{
//using (db)
get{ return Data.Value.ToString();//Convert to byte[] then to string }
}
}
这本身工作得很好,但我有多个类可以执行相同的交互。当然,我想要一个接口或抽象类,如下所示:
interface Binary<T>
{
void Add(T t);
T Value {get;}
}
这是我的问题:在 Value getter 中,我有一个 linq 查询,它实际上返回带有 Linq.Binary Value
的 LinqToSql Data 类,但是在我的界面中,我有一个 T Value
。如何将 linq 查询转换为此 Binary 接口?像这样的东西,虽然它不起作用:
List<Binary<T>> Value
{
get
{
return (from D in db.Data
select D).Cast<Binary<T>>();
}
}
编辑:我最后有错误的属性。它应该是值,而不是名称。
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 perfectly 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非您希望您的
Data
类本身是通用的,否则它必须为特定的T
实现Binary
,即string
,因此类声明如下所示:在这种情况下,Name 实现如下所示:
Unless you want your
Data
class to be generic itself, it has to implementBinary<T>
for a particularT
, i.e.string
, so the class declaration looks like this:In that case the Name implementation looks something like this: