连接的服务属性应该导致数据源 [英] Connected service property should lead to datasource
问题描述
大家好,
我有连接服务并使用属性从EF6检索数据。
在测试应用程序中,我可以创建带有这些属性名称的列表,用于填充组合框
Hi everyone,
I have a connected services and use properties to retrieve data from EF6.
In a testapplication I can create a list with these propertyname to fill a combobox
ConnectedDataServices.DataServiceContext.GetType().GetRuntimeProperties().Select(x => x.Name).ToList();
我可以从他的属性获取vales但是作为类型对象而不是原始类型ConcurrentBag<> ;.
但是如何获取对象的内容或值来绑定所需的concurrentbag?
我尝试了什么: < br $>
I can get the vales from he property but as type object instead of the original type ConcurrentBag<>.
But how do I get the contents or values of the object to bind the concurrentbag desired?
What I have tried:
var selectedProp = comboBox1.SelectedItem.ToString();
PropertyInfo propInfo = _availableProps.FirstOrDefault(x => x.Name == selectedProp);
var results = propInfo.GetValue(ConnectedDataServices.DataServiceContext, null);
Type type = results.GetType();
dataGridView1.DataSource = results;
dataGridView1.Refresh();
结果现在是对象类型而不是ConcurrentBag< entity>
results is now object type instead of ConcurrentBag<entity>
推荐答案
如果您使用的是最新版本的C#编译器,则可以使用模式匹配开关
来确保属性值的类型正确:
If you're using a recent version of the C# compiler, you can use a pattern-matchingswitch
to make sure the property value is of the correct type:
switch (results)
{
case null:
case IList _:
case IListSource _:
{
// Includes IBindingList and IBindingListView
dataGridView1.DataSource = results;
break;
}
case ICollection collection:
{
dataGridView1.DataSource = new ArrayList(collection);
break;
}
case IEnumerable sequence:
{
dataGridView1.DataSource = sequence.Cast<object>().ToList();
break;
}
default:
{
// Not a supported type:
dataGridView1.DataSource = null;
break;
}
}
将它转换为在较旧的编译器中工作会很简单,但它不会像干净。:)
这篇关于连接的服务属性应该导致数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!