我在Vb.net中的Sql查询是这样的:
Dim TableName As String ="City"
Dim Str As String="Select * from "+TableName+""我从另一个表单.how得到了TableName,我们可以在linq中做这个吗?我们可以在Linq查询中动态使用TableName吗?
请帮帮我?
发布于 2009-01-24 13:12:40
很抱歉我的例子是用C#编写的,但是我的Vb已经生疏了。我能读懂它,但不能很好地写在我的头上。
我能想到的最好的方法是使用反射和扩展方法,而不是LINQ语法。
PropertyInfo info = dataContext.GetType().GetProperty( "City" );
IQueryable table = info.GetValue( dataContext, null ) as IQueryable;
var query = table.Where( t => t.Column == "Value" )
.Select( t => t.OtherColumn );在本例中,我假设为LINQtoSQL,因此我将从datacontext中获取对象。如果您使用的是其他对象,则需要调整查找对象的方法。
您还可以根据表名的值使用一堆if-then语句。如果可能的表的数量是固定的并且很小,这可能会更好。
发布于 2009-12-18 18:32:57
我有了这个想法,谢谢,非常有帮助,但是我不能看到那些属性。
string linqObjectName = "Test";
PropertyInfo info = db.GetType().GetProperty(linqObjectName);
IQueryable table = info.GetValue(db, null) as IQueryable;
PropertyInfo[] properties = table.GetType().GetProperties();
ltr.Text += "properties: <hr size=1/>";
foreach (PropertyInfo p in properties)
{
ltr.Text += p.Name + "<br />";
}这就是结果;
属性:
上下文IsReadOnly
编辑:我找到了!
PropertyInfo[] properties =
table.Where("WebRef = \"" + webref + "\"").ElementType.GetProperties(); https://stackoverflow.com/questions/475984
复制相似问题