LINQ对动态对象 [英] LINQ against dynamic object
问题描述
我已经实现了导入工具数据使用Linq-to-Sql进入完整数据库,我想重新使用导入到轻量级数据库的逻辑。
我的想法是为了这个目的使用动态
对象。
所以我添加了两个不同的Linq-to-SQL .dbml
文件到我的项目,并使用相应的表填充它们。我设置它们使用不同的命名空间来避免名称冲突。
我没有问题来初始化 DbContext
(at至少我没有得到任何编译器错误在这里)动态变量:
bool _usefirstdb;
dynamic _db;
if(_usefirstdb)
{
_db = new FirstDBDataContext(string.Format(SqlScripts.SqlServerConnectionString,args [1],args [2]));
}
else
{
_db = new SecondDBDataContext(string.Format(SqlScripts.SqlServerConnectionString,args [1],args [2]));
}
但是我对这个对象运行LINQ查询有问题:
var query = from inst in _db.Instances
其中inst.Name.Equals(args [3])
select inst.Id;
这是因为 inst
的类型不能由编译器确定(而不是动态
)。
有什么办法可以克服这个问题吗?我知道C#是静态类型的,但我没有看到任何其他方式重新使用我的代码...
当您的两个DbContext类都实现相同的接口时,您可以使用该接口的引用。在您的界面中,保持DbSet的两个上下文都有。然后当您使用linq时,您只能查询这些集合,并且仍然是类型安全的。
像这样:
public interface IInstances
{
DbSet< Instance>实例{get;
}
public FirstDBDataContext:DbContext,IInstances
{
//正常实现
}
public SecondDBDataContext:DbContext ,IInstances
{
//正常执行
}
bool _usefirstdb;
IInstances _db;
if(_usefirstdb)
{
_db = new FirstDBDataContext(string.Format(SqlScripts.SqlServerConnectionString,args [1],args [2]));
}
else
{
_db = new SecondDBDataContext(string.Format(SqlScripts.SqlServerConnectionString,args [1],args [2]));
}
var query = from inst in _db.Instances
其中inst.Name.Equals(args [3])
select inst.Id;
I am implementing an import tool that reads data from file and stores them to a database. We have one database in two different editions: a "full" edition and a "lightweight" edition. The lightweight edition has one table less plus four other tables which reference the missing table also don't have this foreign key column.
I have already implemented the tool to import the data into the "full" database using Linq-to-Sql and I want to re-use the logic for the import to the lightweight version of the database.
My idea was to use dynamic
objects for this purposes.
So I added two different Linq-To-SQL .dbml
files to my project and populated them with the respective table. I set them to use different namespaces to avoid name clashing.
I have no problems to initialize the DbContext
(at least I don't get any compiler error here) of the dynamic variable:
bool _usefirstdb;
dynamic _db;
if (_usefirstdb)
{
_db = new FirstDBDataContext (string.Format(SqlScripts.SqlServerConnectionString, args[1], args[2]));
}
else
{
_db = new SecondDBDataContext(string.Format(SqlScripts.SqlServerConnectionString,args[1], args[2]));
}
but I got problems with running LINQ queries against this objects:
var query = from inst in _db.Instances
where inst.Name.Equals(args[3])
select inst.Id;
This is because the type of inst
cannot be determined by the compiler (and is not dynamic
).
Is there any way to overcome this problem? I know that C# is statically type, but I don't see any other way to re-use my code...
When both of your DbContext classes implement the same interface you can have the reference as that interface. In your interface you keep the DbSet's that both of your contexts have. Then when you use linq you can query only those sets and it is still type safe.
Like so:
public interface IInstances
{
DbSet<Instance> Instances { get; }
}
public FirstDBDataContext :DbContext, IInstances
{
//Normal implementation
}
public SecondDBDataContext :DbContext, IInstances
{
//Normal implementation
}
bool _usefirstdb;
IInstances _db;
if (_usefirstdb)
{
_db = new FirstDBDataContext (string.Format(SqlScripts.SqlServerConnectionString, args[1], args[2]));
}
else
{
_db = new SecondDBDataContext(string.Format(SqlScripts.SqlServerConnectionString,args[1], args[2]));
}
var query = from inst in _db.Instances
where inst.Name.Equals(args[3])
select inst.Id;
这篇关于LINQ对动态对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!