Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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#_Linq - Fatal编程技术网

C# 泛型列表第一个默认值

C# 泛型列表第一个默认值,c#,linq,C#,Linq,我有一个通用列表,并试图根据值获取项目,即 list.FirstOrDefault(u=>u.Key == key) // error 'T' does not contain definition for 'Key' 泛型类型T具有不同的基类。根据T在列表中的填充位置,您可以在其上指定泛型约束以具有接口(或基类) 接口: interface IHasKey { string Key { get; } } // or some other type for `Key` 一般约束: wh

我有一个通用列表,并试图根据值获取项目,即

list.FirstOrDefault(u=>u.Key == key) // error 'T' does not contain definition for 'Key'

泛型类型
T
具有不同的基类。

根据
T
列表中的填充位置,您可以在其上指定泛型约束以具有接口(或基类)

接口:

interface IHasKey { string Key { get; } } // or some other type for `Key`
一般约束:

where T : IHasKey

根据您的
T
列表中的填充位置,您可以在其上指定通用约束以具有接口(或基类)

接口:

interface IHasKey { string Key { get; } } // or some other type for `Key`
一般约束:

where T : IHasKey

您试图在未指定T时使用Key,因此我们不知道T类是否包含任何字段/属性键。 您可以做的一件事是使用抽象类/接口,或者尝试将u强制转换到包含“Key”的类中(假设您具体需要一些类)。为了获得更准确的答案,需要更多关于您的列表及其项目的详细信息。
希望能有帮助

您试图在未指定T时使用Key,因此我们不知道T类是否包含任何字段/属性Key。 您可以做的一件事是使用抽象类/接口,或者尝试将u强制转换到包含“Key”的类中(假设您具体需要一些类)。为了获得更准确的答案,需要更多关于您的列表及其项目的详细信息。
希望能有帮助

使用泛型方法时,必须确保T具有属性
。这可以通过一般约束实现。可以是基类或接口:

interface IKeyedObject {
    string Key { get; };
}

class BaseWithKey : IKeyedObject {
    public string Key { get; set; };
}

class DerivedA : BaseWithKey {
}

class DerivedB : BaseWithKey {
}

class OtherWithKey : IKeyedObject {
    public string Key { get; set; };
}

//Solution with base class (will work with BaseWithKey, DerivedA, DerivedB)

T GetItemBaseClass<T>(List<T> list, string key)
    where T : BaseWithKey {

    return list.FirstOrDefault(u=>u.Key == key);
}

//Solution with interface (will work with all classes)

T GetItemInterface<T>(List<T> list, string key)
    where T : IKeyedObject {

    return list.FirstOrDefault(u=>u.Key == key);
}
接口IKeyDobject{
字符串键{get;};
}
类BaseWithKey:IKeyedObject{
公共字符串密钥{get;set;};
}
派生类:BaseWithKey{
}
类DerivedB:BaseWithKey{
}
类OtherWithKey:IKeyDobject{
公共字符串密钥{get;set;};
}
//具有基类的解决方案(将与BaseWithKey、DerivedA、DerivedB一起使用)
T GetItemBaseClass(列表,字符串键)
其中T:BaseWithKey{
返回list.FirstOrDefault(u=>u.Key==Key);
}
//带接口的解决方案(适用于所有类)
T GetItemInterface(列表、字符串键)
其中T:Ikeydobject{
返回list.FirstOrDefault(u=>u.Key==Key);
}

使用泛型方法时,必须确保T具有属性
。这可以通过一般约束实现。可以是基类或接口:

interface IKeyedObject {
    string Key { get; };
}

class BaseWithKey : IKeyedObject {
    public string Key { get; set; };
}

class DerivedA : BaseWithKey {
}

class DerivedB : BaseWithKey {
}

class OtherWithKey : IKeyedObject {
    public string Key { get; set; };
}

//Solution with base class (will work with BaseWithKey, DerivedA, DerivedB)

T GetItemBaseClass<T>(List<T> list, string key)
    where T : BaseWithKey {

    return list.FirstOrDefault(u=>u.Key == key);
}

//Solution with interface (will work with all classes)

T GetItemInterface<T>(List<T> list, string key)
    where T : IKeyedObject {

    return list.FirstOrDefault(u=>u.Key == key);
}
接口IKeyDobject{
字符串键{get;};
}
类BaseWithKey:IKeyedObject{
公共字符串密钥{get;set;};
}
派生类:BaseWithKey{
}
类DerivedB:BaseWithKey{
}
类OtherWithKey:IKeyDobject{
公共字符串密钥{get;set;};
}
//具有基类的解决方案(将与BaseWithKey、DerivedA、DerivedB一起使用)
T GetItemBaseClass(列表,字符串键)
其中T:BaseWithKey{
返回list.FirstOrDefault(u=>u.Key==Key);
}
//带接口的解决方案(适用于所有类)
T GetItemInterface(列表、字符串键)
其中T:Ikeydobject{
返回list.FirstOrDefault(u=>u.Key==Key);
}

您需要创建一个接口,该接口上有一个
Key
属性,并让您想要使用此方法的所有类实现它。只有在执行GroupBy()时,linq才会生成“Key”参数。您的列表是否来自GroupBy()方法?能否显示列表的声明?您需要创建一个具有
Key
属性的接口,并让希望使用此方法的所有类实现它。“Key”参数仅由linq在执行GroupBy()时生成。您的列表是否来自GroupBy()方法?能否显示列表的声明?