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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.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_Iqueryable - Fatal编程技术网

C# 如何在子查询中将字符串变量设置为属性

C# 如何在子查询中将字符串变量设置为属性,c#,linq,iqueryable,C#,Linq,Iqueryable,我有以下目标: var authors1 = new List<Author>() { new Author{ FirstName = "William", LastName = "Smith" }, new Author{ FirstName = "Fred", LastName = "Jones" } }; var authors2 = new List<Author>(

我有以下目标:

var authors1 = new List<Author>() {
    new Author{ FirstName = "William", LastName = "Smith" },
    new Author{ FirstName = "Fred", LastName = "Jones" }
};

var authors2 = new List<Author>() {
    new Author{ FirstName = "Brian", LastName = "Brains" },
    new Author{ FirstName = "Billy", LastName = "TheKid" }
};

var books = new List<Book>() {
    new Book{ Title = "JAVA", Description = "Description Java", Authors = authors1 },
    new Book{ Title = "PHP", Description = "Description PHP", Authors = authors2 },
};
var authors1=新列表(){
新作者{FirstName=“William”,LastName=“Smith”},
新作者{FirstName=“Fred”,LastName=“Jones”}
};
var authors2=新列表(){
新作者{FirstName=“Brian”,LastName=“Brains”},
新作者{FirstName=“Billy”,LastName=“TheKid”}
};
var books=新列表(){
新书{Title=“JAVA”,Description=“Description JAVA”,Authors=authors1},
新书{Title=“PHP”,Description=“Description PHP”,Authors=authors2},
};
我想创建按作者筛选的子查询。我知道我可以这样做:

IEnumerable<Book> list = books.Where(x => x.Authors.Where(j => j.FirstName == "William").Any());
IEnumerable list=books.Where(x=>x.Authors.Where(j=>j.FirstName==“William”).Any());
但是我想使用authors属性作为字符串变量

var entity = "Authors"
IEnumerable<Book> list = books.Where(x => x[entity].Where(j => j.FirstName == "William").Any());
var entity=“作者”
IEnumerable list=books.Where(x=>x[entity].Where(j=>j.FirstName==“William”).Any();

这不起作用。

我不明白你为什么要这样做。。。但这里有一种方法可以通过反射达到你想要的效果:

public bool HasAuthorWithName(Book book, string authName)
{
    // Retrieve list of class' properties
    var p = book.GetType().GetProperties();
        
    // Get list of authors prop by name             
    var entity = "Authors";
    var lstAuthors = p.First(x => x.Name == entity).GetValue(book, null) as List<Author>;
        
    // Filter by author's name
    if (lstAuthors != null && lstAuthors.Any())
    {
        return lstAuthors.Any(j => j.FirstName == authName);
    }
    
    return false;
}
public bool HasAuthorWithName(Book Book,string authName)
{
//检索类的属性列表
var p=book.GetType().GetProperties();
//按名称获取作者列表
var entity=“作者”;
var lstAuthors=p.First(x=>x.Name==entity).GetValue(book,null)作为列表;
//按作者姓名筛选
if(lstAuthors!=null&&lstAuthors.Any())
{
返回lstAuthors.Any(j=>j.FirstName==authName);
}
返回false;
}
现在可以调用此方法:

IEnumerable<Book> list = books.Where(x => HasAuthorWithName(x, "William"));
IEnumerable list=books.Where(x=>HasAuthorWithName(x,“William”);

我不明白你为什么要这样做。。。但这里有一种方法可以通过反射达到你想要的效果:

public bool HasAuthorWithName(Book book, string authName)
{
    // Retrieve list of class' properties
    var p = book.GetType().GetProperties();
        
    // Get list of authors prop by name             
    var entity = "Authors";
    var lstAuthors = p.First(x => x.Name == entity).GetValue(book, null) as List<Author>;
        
    // Filter by author's name
    if (lstAuthors != null && lstAuthors.Any())
    {
        return lstAuthors.Any(j => j.FirstName == authName);
    }
    
    return false;
}
public bool HasAuthorWithName(Book Book,string authName)
{
//检索类的属性列表
var p=book.GetType().GetProperties();
//按名称获取作者列表
var entity=“作者”;
var lstAuthors=p.First(x=>x.Name==entity).GetValue(book,null)作为列表;
//按作者姓名筛选
if(lstAuthors!=null&&lstAuthors.Any())
{
返回lstAuthors.Any(j=>j.FirstName==authName);
}
返回false;
}
现在可以调用此方法:

IEnumerable<Book> list = books.Where(x => HasAuthorWithName(x, "William"));
IEnumerable list=books.Where(x=>HasAuthorWithName(x,“William”);

为什么不使用或调节。其中(x=>x.Authors(…)| | x.Title(…)| | x.Description(…)您试图解决什么问题?看见其他对象/属性是否可以搜索“William”?这是一个特定的案例。这将是一个通用的搜索。这就是我想使用变量的原因。为什么不使用或条件呢。其中(x=>x.Authors(…)| | x.Title(…)| | x.Description(…)您试图解决什么问题?看见其他对象/属性是否可以搜索“William”?这是一个特定的案例。这将是一个通用的搜索。这就是我想使用变量的原因。