从上下文将多个结​​果附加到一个列表(结果)不起作用C#

时间:2019-07-18 07:21:34

标签: c# entity-framework linq

我正在开发小型.net应用,我需要按其类型获取文章,有3种可能的类型:

  • 食物
  • 糖果

我想知道如果我想在与食物和饮料同列的文章中怎么办,如何将上下文结果添加到另一个结果中而不重置它。.

这是我当前的代码:

public async Task<IEnumerable<Article>> GetArticlesByType(ArticleObject request)
{
var result = new Article[] { }.AsQueryable();
IQueryable<ArticleDTO> query = null;

if (request.Food.HasValue && (bool)request.Food)
{
    // Return type of query is IQueryable<Article> 
    query = _context.Articles.Where(x => x.Active == true && x.ArticleType == ArticleType.Food).Select(x => new Article
    {
        Id = x.Id,
        ArticleName = x.ArticleName
    });
    // Here I just wanted if this condition is satisfied to add values to my result
    result.AsQueryable().Union(query);
}

if (request.Drink.HasValue && (bool)request.Drink)
{
    query = _context.Articles.Where(x => x.Active == true && x.ArticleType == ArticleType.Drink).Select(x => new Article
    {
        Id = x.Id,
        ArticleName = x.ArticleName
    });
    // Again if there are any values in query add them to existing result values
    result.AsQueryable().Union(query);
}

if (request.Candy.HasValue && (bool)request.Candy)
{
    // When its candy I want also articles from food category
    query = _context.Articles.Where(x => x.Active == true && x.ArticleType == ArticleType.Food || x.ArticleType==ArticleType.Candy).Select(x => new Article
    {
        Id = x.Id,
        ArticleName = x.ArticleName
    });
    // Again if there are values in query add them to existing result 
    result.AsQueryable().Union(query);
}
//At the end return result and all the values in case all conditions were satisfied
return await result.ToListAsync();

}

现在,我每个人都尝试将值添加到result变量中,但是即使query有一些数据,所有时间结果都是空的。

查询返回IQueryable<Article>,所以我无法创建任何接口的空列表,因为我无法对其进行详细说明。

谢谢大家 干杯

谢谢大家

欢呼

1 个答案:

答案 0 :(得分:1)

    public async Task<IEnumerable<Article>> GetArticlesByType(ArticleObject request)
{
    IQueryable<ArticleDTO> foodArticles= null;
    IQueryable<ArticleDTO> drinkArticles= null;
    IQueryable<ArticleDTO> candyArticles= null;

    if (request.Food.HasValue && (bool)request.Food)
    {
        foodArticles= _context.Articles.Where(x => x.Active == true && x.ArticleType == ArticleType.Food).Select(x => new Article
        {
            Id = x.Id,
            ArticleName = x.ArticleName
        });
    }

    if (request.Drink.HasValue && (bool)request.Drink)
    {
        drinkArticles= _context.Articles.Where(x => x.Active == true && x.ArticleType == ArticleType.Drink).Select(x => new Article
        {
            Id = x.Id,
            ArticleName = x.ArticleName
        });
    }

    if (request.Candy.HasValue && (bool)request.Candy)
    {
        // When its candy I want also articles from food category
        candyArticles= _context.Articles.Where(x => x.Active == true && x.ArticleType == ArticleType.Food || x.ArticleType==ArticleType.Candy).Select(x => new Article
        {
            Id = x.Id,
            ArticleName = x.ArticleName
        });
    }
    return await foodArticles.AsQueryable().Union(drinkArticles).AsQueryAble().Union(candyArticles);
}