Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.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# 基于多个参数筛选列表,并将结果收集到另一个列表中_C#_Linq_List - Fatal编程技术网

C# 基于多个参数筛选列表,并将结果收集到另一个列表中

C# 基于多个参数筛选列表,并将结果收集到另一个列表中,c#,linq,list,C#,Linq,List,我有一个物品清单。对象具有以下属性: public string mCardColor { get; set; } public string mCardType { get; set; } public string mCardRarity { get; set; } 在我看来,我有可能直接过滤通过搜索引擎使用下拉列表获得的列表 然后,我将过滤器的值传递给控制器方法,并检查该请求是否为实际的Ajax请求,如下所示: public ActionResult DisplayCardsResu

我有一个物品清单。对象具有以下属性:

public string mCardColor { get; set; }

public string mCardType { get; set; }

public string mCardRarity { get; set; }
在我看来,我有可能直接过滤通过搜索引擎使用下拉列表获得的列表

然后,我将过滤器的值传递给控制器方法,并检查该请求是否为实际的Ajax请求,如下所示:

public ActionResult DisplayCardsResults(string _rarity = "", string _type = "", string _color = "")
{
    ViewBag._rarity = _rarity;
    ViewBag._color = _color;
    ViewBag._type = _type;

    if (Request.IsAjaxRequest())
    {
        mListCardColors = null;
        mListCardType = null;
        mListCardRarity = null;

        if (_rarity != "All")
        {
            mListCardRarity = mListCards.Where(_item => _item.mMasterCard.mCardRarity == _rarity).ToList();
        }
        if (_type != "All")
        {
            mListCardType =
                mListCards.Where(_item => _item.mMasterCard.mCardType.ToLower().Contains(_type.ToLower())).ToList();
        }
        if (_color != "All")
        {
            mListCardColors = mListCards.Where(_item => _item.mMasterCard.mCardColor == _color).ToList();
        }

        if (mListCardType == null && mListCardColors == null && mListCardRarity == null)
        {
            return PartialView("_ResultsTable", mListCards.ToPagedList(pageNumber, ValueDomain.PAGE_SIZE));
        }

        mListCardsToShow = new List<CardDisplay>();

        if (mListCardType != null)
        {
            mListCardsToShow.AddRange(mListCardType);
        }
        if (mListCardRarity != null)
        {
            mListCardsToShow.AddRange(mListCardRarity);
        }
        if(mListCardColors != null)
        {
            mListCardsToShow.AddRange(mListCardColors);
        }

        return PartialView("_ResultsTable", mListCardsToShow.ToPagedList(pageNumber, ValueDomain.PAGE_SIZE));
    }

    if (mListCardsToShow.Count > 0)
    {
        mListCardsToShow = SortListOrder(_sortOrder, mListCardsToShow);
        return View(mListCardsToShow.ToPagedList(pageNumber, ValueDomain.PAGE_SIZE));
    }

    if (mListCards.Count > 0)
    {
        mListCards = SortListOrder(_sortOrder, mListCards);
    }

    return View(mListCards.ToPagedList(pageNumber, ValueDomain.PAGE_SIZE));
}
public ActionResult displayscardsresults(字符串_-rarity=“”、字符串_-type=“”、字符串_-color=“”)
{
ViewBag.\u稀有=\u稀有;
视图包。_颜色=_颜色;
视图包。_类型=_类型;
if(Request.IsAjaxRequest())
{
mListCardColors=null;
mListCardType=null;
mListCardRarity=null;
如果(_稀有性!=“全部”)
{
mListCardRarity=mListCards.Where(_item=>_item.mMasterCard.mCardRarity==_rarity).ToList();
}
如果(_type!=“全部”)
{
mListCardType=
mListCards.Where(_item=>_item.mMasterCard.mCardType.ToLower()。包含(_type.ToLower()).ToList();
}
如果(_color!=“全部”)
{
mListCardColors=mListCards.Where(_item=>_item.mMasterCard.mCardColor==_color.ToList();
}
if(mListCardType==null&&mListCardColors==null&&mListCardRarity==null)
{
返回PartialView(“_ResultsTable”,mListCards.ToPagedList(pageNumber,ValueDomain.PAGE_SIZE));
}
mListCardsToShow=新列表();
if(mListCardType!=null)
{
mListCardsToShow.AddRange(mListCardType);
}
if(mListCardRarity!=null)
{
mListCardsToShow.AddRange(mListCardRarity);
}
if(mListCardColors!=null)
{
mListCardsToShow.AddRange(mListCardColors);
}
返回PartialView(“_ResultsTable”,mListCardsToShow.ToPagedList(pageNumber,ValueDomain.PAGE_SIZE));
}
如果(mListCardsToShow.Count>0)
{
mListCardsToShow=排序器(_sortOrder,mListCardsToShow);
返回视图(mListCardsToShow.ToPagedList(pageNumber,ValueDomain.PAGE_SIZE));
}
如果(mListCards.Count>0)
{
mListCards=排序器(_sortOrder,mListCards);
}
返回视图(mListCards.ToPagedList(页码、ValueDomain.PAGE_大小));
}
您有两个列表:
mListCards
是从搜索引擎获得的卡片列表。这不需要改变。只有当请求是Ajax请求时,才会使用
mListCardsToShow

我只希望根据传递给控制器方法的过滤器保留所需的值。原理如下:如果三个下拉列表全部打开,则显示所有卡。但是,如果在任何或所有dropdownlist中有一个值,则需要过滤实际列表


除了编写9种不同的场景外,是否有一种有效的方法可以使用Linq根据三个参数过滤列表?

如果您没有充分的理由不这样做,那么同时过滤三个字段可能是有意义的:

var filteredCards =
    from card in mListCards
    where _color == "ALL" || card.mCardColor == _color
    where _type == "ALL" || card.mCardType == _type
    where _rarity == "ALL" || card.mCardRarity == _rarity
    select card;

但是你的代码正好相反,有3个顺序过滤器,不是吗?安德鲁,我会试试你的答案,然后带着结果回来。@SargeBorsch:除非你期望过滤器的计算并行进行,否则从某种意义上讲,即使它们都在“一”中,它也总是顺序的安德鲁:这正是我所希望的。谢谢@AndrewCoonce当然,是的,但是您将有3个相互调用的委托,而不是1个,并且方法调用会增加明显的开销。