Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.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/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# 从Linq查询中的接口访问派生类的属性?_C#_Linq_Asp.net Core Webapi - Fatal编程技术网

C# 从Linq查询中的接口访问派生类的属性?

C# 从Linq查询中的接口访问派生类的属性?,c#,linq,asp.net-core-webapi,C#,Linq,Asp.net Core Webapi,我创建了一个接口,它有两个派生类,如下面的代码片段所示。我想在Linq查询中访问这些派生类的成员,但我无法做到这一点 模型类结构: public interface IDirDetails { } public class DirectoryDetails: IDirDetails { public string[] PathElements { get; set; } public string[] ApplicationList { get; set; } p

我创建了一个接口,它有两个派生类,如下面的代码片段所示。我想在
Linq
查询中访问这些派生类的成员,但我无法做到这一点

模型类结构:

public interface IDirDetails
{   
}
public class DirectoryDetails: IDirDetails
{
    public string[] PathElements { get; set; }
    public string[] ApplicationList { get; set; }

    public List<DirectoryItemInfo> Items { get; set; }
}
public class DirectoryItemInfo: IDirDetails
{
    public string FileName { get; set; }
    public string FileType { get; set; }
    public string FileSize { get; set; }
    public string FileIcon { get; set; }
    public DateTime FileModified { get; set; }
    public string FilePath { get; set; }
}

有人能建议我如何从接口访问派生类所需的成员吗?

对你的问题的简单回答是:只需强制转换即可

data = data.Cast<DirectoryItemInfo>().Where(p => p.FileName.ToString().ToLower().Contains(search.ToLower()) ||  
                       p.FileSize.ToLower().Contains(search.ToLower()) ||             
                       p.FileModified.ToString().ToLower().Contains(search.ToLower())).ToList(); 
正确答案是:您的模型有问题。您不需要接口列表,而是需要特定类型的列表,或者您应该将这些属性移动到接口

例如,您的
LoadData
方法返回接口列表,但在您的示例中,您将其用作特定类的列表。您可以将其更改为:

public List<T> LoadData<T>()  where T:IDirDetails
{   
    // return specific List
}
public List LoadData(),其中T:IDirDetails
{   
//返回特定列表
}
在此之后,您可以将其称为:

List<DirectoryItemInfo> data = LoadData<DirectoryItemInfo>();
List data=LoadData();

你不需要演员。在
LoadData
的某个地方,您应该决定要返回哪些元素。

您可以使用'OfType'方法从'data'列表中获取'DirectoryItemInfo'类型的所有项目,并将'Where'的结果强制转换回'idiredetails'列表

data = data.OfType<DirectoryItemInfo>()
           .Where(p => 
                  p.FileName.ToString().ToLower().Contains(search.ToLower()) ||
                  p.FileSize.ToLower().Contains(search.ToLower()) ||  
                  p.FileModified.ToString().ToLower().Contains(search.ToLower()))
           .ToList<IDirDetails>();
data=data.OfType()
.其中(p=>
p、 FileName.ToString().ToLower().Contains(search.ToLower())||
p、 FileSize.ToLower().Contains(search.ToLower())|
p、 FileModified.ToString().ToLower().Contains(search.ToLower()))
.ToList();

您只能访问由接口定义的成员。如果需要访问该接口的具体实现的成员,则需要具体实现的实例(在您的情况下,需要
directorydeail
DirectoryItemInfo
的实例)


您可以像其他成员所说的那样使用强制转换做一些事情,但是您仍然以某种方式访问具体实现的实例。

使用类型的
来过滤类型

List<IDirDetails> data = LoadData();                   
int totalRecords = data.Count;

if (!string.IsNullOrEmpty(search) && !string.IsNullOrWhiteSpace(search))
{
    // Apply search
    data = data.OfType<DirectoryItemInfo>().Where(p => p.FileName.ToString().ToLower().Contains(search.ToLower()) ||  
                           p.FileSize.ToLower().Contains(search.ToLower()) ||               
                           p.FileModified.ToString().ToLower().Contains(search.ToLower())).Cast<IDirDetails>().ToList(); 
}   
List data=LoadData();
int totalRecords=data.Count;
如果(!string.IsNullOrEmpty(搜索)和&!string.IsNullOrWhiteSpace(搜索))
{
//应用搜索
data=data.OfType()。其中(p=>p.FileName.ToString().ToLower()。包含(search.ToLower())|
p、 FileSize.ToLower().Contains(search.ToLower())|
p、 FileModified.ToString().ToLower().Contains(search.ToLower()).Cast().ToList();
}   

Cast
如果列表中有
DirectoryDetails
则无效。谢谢你的回答,先生,你能建议实现这些的正确方法吗?@RahulNagrale我看不到你的全部代码,我在回答中提出了一个建议,但这取决于你到底在做什么。我回来时它显示了一个错误
List
from
LoadData()
,错误:
CS0029无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”
@RahulNagrale确实如此。您可以返回DirectoryItemInfo列表并将其用作DirectoryItemInfo。或者返回接口列表并将其用作接口。先生,我遵循了您更新的代码已在回答中提供,即将
LoadData()
修改为
public List LoadData(),其中T:IDirDetails
,现在也返回
List
,但仍显示错误。请查看错误:
CS0029无法隐式将类型“System.Collections.Generic.List”转换为“System.Collections.Generic.List”
List<DirectoryItemInfo> data = LoadData<DirectoryItemInfo>();
data = data.OfType<DirectoryItemInfo>()
           .Where(p => 
                  p.FileName.ToString().ToLower().Contains(search.ToLower()) ||
                  p.FileSize.ToLower().Contains(search.ToLower()) ||  
                  p.FileModified.ToString().ToLower().Contains(search.ToLower()))
           .ToList<IDirDetails>();
List<IDirDetails> data = LoadData();                   
int totalRecords = data.Count;

if (!string.IsNullOrEmpty(search) && !string.IsNullOrWhiteSpace(search))
{
    // Apply search
    data = data.OfType<DirectoryItemInfo>().Where(p => p.FileName.ToString().ToLower().Contains(search.ToLower()) ||  
                           p.FileSize.ToLower().Contains(search.ToLower()) ||               
                           p.FileModified.ToString().ToLower().Contains(search.ToLower())).Cast<IDirDetails>().ToList(); 
}