簡體   English   中英

使用列表中的任何項目過濾 IQueryable

[英]Filter IQueryable with any item from the list

所以我有一個數據庫,我正在嘗試向 QueryBuilder 添加一個方法,該方法將返回 IQueryable,如下所示:

WHERE
student.Name == "Alan" OR student.Name == "Zoe" OR student.Name == "X" OR ...

我有一個可以是任意大小的名稱列表。

代碼如下所示:

public class QueryBuilder
{
    private SudentData context;
    private IQueryable<Students> query;

    public QueryBuilder(StudentData context)
    {
        context = context;
        query = context.Students;
    }

    public QueryBuilder WithDateFrom(DateTime date)
    {
        query = query.Where(x => x.Created >= date);
        return this;
    }

    public QueryBuilder WithName(List<string> names)
    {
        if (name.Any())
        {
            //Missing query
            return this;   
        }
        else
        {
            return this;
        }
    }

    public IQueryable<StudentDataModel> Build()
    {
        return query.Select(x => new StudentDataModel()
        {
            Name = x.Name,
            DateTo = x.DateTo,
            DateFrom = x.DateFrom
        });
    }
}

你可以使用這個:

public QueryBuilder WithName(List<string> names)
{
    if (name.Any())
    {
        query = query.Where(x => names.Contains(x.Name));
        return this;
    }
    else
    {
        return this;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM