使用 LINQ 统一 SQL getter
我得到了许多具有相同设计的不同 SQL 表 - 所有表都具有标识和两个具有相同名称的字符串字段。我不想编写一组函数来从这些表中获取值,我想要一个以表作为参数的过程。但是当我开始检索数据时,它说“无法转换类型 bla-bla-bla”。它需要直接传递类型,这就是我想避免的。该怎么办?
/*
defined tables:
create table tableA
(
id int identity not null,
type_code nvarchar(50) not null,
type_description nvarchar(1000) not null
)
same SQL for tableB and tableC
tableA, tableB, tableC
*/
void getAnyId( Table tbl, string codeFilter)
{
var p=(tableA)tbl; // HERE I GET EXCEPTION !!!
var id = p.Where( r=> r.code == codeFilter);
if( id.Count() != 1 )
return null;
return id.id;
}
另一个例子:
public Dictionary<string,string> readDataSchemeTypes( tvbaseDataContext dc )
{
Dictionary<string,string> ds = new Dictionary<string,string>();
foreach( var ast in dc.tableA)
ds.Add( ast.type_code, ast.type_description );
return ds;
}
这可行,但我需要一组函数,每个表一个。
public Dictionary<string, string> readAnySchemeTypes<T>(System.Data.Linq.Table<T> table) where T:System.Data.Linq.ITable
{
Dictionary<string, string> ds = new Dictionary<string, string>();
foreach (var ast in table)
ds.Add(ast.type_code, ast.type_description); // type_code and type_description are not defined for type T
return ds;
}
这个例子不能编译。
I got many different SQL tables with the same design - all have identity and two string fields with the same names. I do not want to write a set of functions to get values from these tables, i want to have one procedure with the table as a parameter. But when i start to retrieve data it says "can not convert type bla-bla-bla". It needs the type to be directly passed and thats what i want to avoid. What to do?
/*
defined tables:
create table tableA
(
id int identity not null,
type_code nvarchar(50) not null,
type_description nvarchar(1000) not null
)
same SQL for tableB and tableC
tableA, tableB, tableC
*/
void getAnyId( Table tbl, string codeFilter)
{
var p=(tableA)tbl; // HERE I GET EXCEPTION !!!
var id = p.Where( r=> r.code == codeFilter);
if( id.Count() != 1 )
return null;
return id.id;
}
Another example:
public Dictionary<string,string> readDataSchemeTypes( tvbaseDataContext dc )
{
Dictionary<string,string> ds = new Dictionary<string,string>();
foreach( var ast in dc.tableA)
ds.Add( ast.type_code, ast.type_description );
return ds;
}
This works but i need a set of functions, one per each table.
public Dictionary<string, string> readAnySchemeTypes<T>(System.Data.Linq.Table<T> table) where T:System.Data.Linq.ITable
{
Dictionary<string, string> ds = new Dictionary<string, string>();
foreach (var ast in table)
ds.Add(ast.type_code, ast.type_description); // type_code and type_description are not defined for type T
return ds;
}
This example doesn't compile.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一次机会:您可以使用动态 SQL 为每个查询传递表名。但这应该是痛苦的,因为您失去了 LINQ 的类型安全性。
例如:
再次注意动态 SQL。在运行之前您看不到任何错误
第二次机会:让您的方法通用。所以你只需要指定每次调用所需的类型,而不需要重写整个方法。
First chance: you can use dynamic SQL to pass table name for each query. But it should be painful because you loss the type-safe of LINQ.
For example:
Once again, be careful with Dynamic SQL. You cannot see any error until you run it
Second chance: let your method generic. So you just have to specify the type needed for each call, not necessary to re-write the whole method.