Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法从用法推断方法的类型参数。尝试显式指定类型参数_C#_Sql Server_List_Datatable - Fatal编程技术网

C# 无法从用法推断方法的类型参数。尝试显式指定类型参数

C# 无法从用法推断方法的类型参数。尝试显式指定类型参数,c#,sql-server,list,datatable,C#,Sql Server,List,Datatable,我正在尝试使用TextChanged()事件在textbox中填充SQL Server中的自动完成文本[WindowsForm,C#] 我有一个部分课程表 TextChanged事件: private void textBoxFilterCName_TextChanged(object sender, EventArgs e) { DataTable dt = FillCustomerName(textBoxFilterCName.Text); List&

我正在尝试使用TextChanged()事件在textbox中填充SQL Server中的自动完成文本<代码>[WindowsForm,C#]

我有一个部分课程表

TextChanged事件:

private void textBoxFilterCName_TextChanged(object sender, EventArgs e)
    {
        DataTable dt = FillCustomerName(textBoxFilterCName.Text);
        List<string> listNames = CustomerName.ConvertDataTableToList(dt);
        textBoxFilterCName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        AutoCompleteStringCollection dataName = new AutoCompleteStringCollection();
        dataName.AddRange(listNames.ToArray());
        textBoxFilterCName.AutoCompleteCustomSource = dataName;
        textBoxFilterCName.AutoCompleteSource = AutoCompleteSource.CustomSource;
    }
public static class CustomerName
{
    private static T CreateItemFromRow<T>(DataRow row, IList<PropertyInfo> properties) where T : new()
    {
        T item = new T();
        foreach (var property in properties)
        {
            if (property.PropertyType == typeof(System.DayOfWeek))
            {
                DayOfWeek day = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), row[property.Name].ToString());
                property.SetValue(item, day, null);
            }
            else
            {
                property.SetValue(item, row[property.Name], null);
            }
        }
        return item;
    }
    public static List<T> ConvertDataTableToList<T>(this DataTable table) where T : new()
    {
        IList<PropertyInfo> properties = typeof(T).GetProperties().ToList();
        List<T> result = new List<T>();

        foreach (var row in table.Rows)
        {
            var item = CreateItemFromRow<T>((DataRow)row, properties);
            result.Add(item);
        }
        return result;            
    }
}
客户名称类:

private void textBoxFilterCName_TextChanged(object sender, EventArgs e)
    {
        DataTable dt = FillCustomerName(textBoxFilterCName.Text);
        List<string> listNames = CustomerName.ConvertDataTableToList(dt);
        textBoxFilterCName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        AutoCompleteStringCollection dataName = new AutoCompleteStringCollection();
        dataName.AddRange(listNames.ToArray());
        textBoxFilterCName.AutoCompleteCustomSource = dataName;
        textBoxFilterCName.AutoCompleteSource = AutoCompleteSource.CustomSource;
    }
public static class CustomerName
{
    private static T CreateItemFromRow<T>(DataRow row, IList<PropertyInfo> properties) where T : new()
    {
        T item = new T();
        foreach (var property in properties)
        {
            if (property.PropertyType == typeof(System.DayOfWeek))
            {
                DayOfWeek day = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), row[property.Name].ToString());
                property.SetValue(item, day, null);
            }
            else
            {
                property.SetValue(item, row[property.Name], null);
            }
        }
        return item;
    }
    public static List<T> ConvertDataTableToList<T>(this DataTable table) where T : new()
    {
        IList<PropertyInfo> properties = typeof(T).GetProperties().ToList();
        List<T> result = new List<T>();

        foreach (var row in table.Rows)
        {
            var item = CreateItemFromRow<T>((DataRow)row, properties);
            result.Add(item);
        }
        return result;            
    }
}
公共静态类客户名称
{
私有静态T CreateItemFromRow(DataRow行,IList属性),其中T:new()
{
T项=新的T();
foreach(属性中的var属性)
{
if(property.PropertyType==typeof(System.DayOfWeek))
{
DayOfWeek day=(DayOfWeek)Enum.Parse(typeof(DayOfWeek),行[property.Name].ToString();
属性.SetValue(项,天,空);
}
其他的
{
property.SetValue(项,行[property.Name],null);
}
}
退货项目;
}
公共静态列表ConvertDataTableToList(此DataTable表),其中T:new()
{
IList properties=typeof(T).GetProperties().ToList();
列表结果=新列表();
foreach(表中的变量行。行)
{
var item=CreateItemFromRow((DataRow)行,属性);
结果.添加(项目);
}
返回结果;
}
}
在调试应用程序时,将执行查询并成功生成结果。但我无法将datatable结果作为列表加载


非常欢迎对此转换的可能建议。

因此,您希望将数据表转换为特定类型的对象列表。对吧?

泛型方法仅从参数推断泛型类型
T
。由于没有类型为
T
的参数,因此需要显式指定它。所以当你写这行的时候:

List<string> listNames = CustomerName.ConvertDataTableToList(dt);
但是,由于您指定了条件
,其中T:new()
,这意味着泛型类型应该有一个无参数构造函数,string不满足这一条件。您正在按名称匹配行,因此您唯一的选择是修改泛型方法或创建如下类:

public class CustomerNameHolder
{
    public string Name { set; get; }
}
然后:

List listNames=CustomerName.ConvertDataTableToList(dt)
.Select(x=>x.Name).ToList();

然后显示此错误{'string'必须是具有公共无参数构造函数的非抽象类型,才能将其用作泛型类型或方法'CustomerName.ConvertDataTableToList(DataTable)')}@Muruga中的参数'T',因为您指定了
其中T:new()
这意味着类型
T
应该有一个无参数构造函数。弦没有,竖起大拇指!他一闪而过。谢谢你提供的关于类型的信息。
List<string> listNames = CustomerName.ConvertDataTableToList<CustomerNameHolder>(dt)
                                     .Select(x=> x.Name).ToList();