如何在c#中按id分组foreach语句

时间:2017-06-05 17:57:32

标签: c# asp.net-mvc foreach

我有一点挑战,我对mvc很新。我的问题是,

如何通过调用表的id对一堆foreach语句进行分组。这是我的代码:

foreach (
                        var owner in
                            _entities.LLAttrDatas.Where(
                                n => n.AttrID == 2 && n.ValStr.Contains(searchInputModel.OwnersName)))
                    {
                        ViewBag.searchName = owner.ValStr;
                    }

                    foreach (
                        var ownerAddress in
                            _entities.LLAttrDatas.Where(
                                n => n.AttrID == 3 && n.ValStr.Contains(searchInputModel.OwnersAddress)))
                    {
                        ViewBag.searchAddress = ownerAddress.ValStr;
                    }

                    foreach (
                        var propertyAddress in
                            _entities.LLAttrDatas.Where(
                                n => n.AttrID == 4 && n.ValStr.Contains(searchInputModel.DescriptionOrLocationOfProp)))
                    {
                        ViewBag.searchProperty = propertyAddress.ValStr;
                    }

                    foreach (
                        var propertyVolumeNo in
                            _entities.LLAttrDatas.Where(
                                n => n.AttrID == 5 && n.ValStr.Contains(searchInputModel.VolumeNumber)))
                    {
                        ViewBag.searchPropertyVolumeNo = propertyVolumeNo.ValStr;
                    }

                    foreach (
                        var propertyPlanNo in
                            _entities.LLAttrDatas.Where(
                                n => n.AttrID == 6 && n.ValStr.Contains(searchInputModel.SurveyPlanNumber))
                        )
                    {
                        ViewBag.searchPropertyPlanNo = propertyPlanNo.ValStr;
                    }

现在,当用户针对上述任何查询进行搜索时,只显示一个结果。我希望它显示来自不同查询的所有结果,因为它们都具有相同的ID。任何帮助将非常感谢。

感谢您的评论和支持。我们每天都学习。无论如何,我找到了一种更好的方法:

public IQueryable<DocumentProperty> GetDocuments(DocumentSearchInputModel searchInputModel)
            {
                Context = new DataContext();

                if (Context != null)
                {
                    var result = Context.DocumentsForSearch.AsQueryable();     
                    if (searchInputModel != null)
                    {

                        result =
                            Context.DocumentsForSearch.Where(
                                d =>
                                    d.DescriptionOrLocationOfProp.Contains(searchInputModel.DescriptionOrLocationOfProp)
                                    || d.OwnersAddress.Contains(searchInputModel.OwnersAddress)
                                    || d.OwnersName.Contains(searchInputModel.OwnersName)
                                    || d.SurveyPlanNumber.Contains(searchInputModel.SurveyPlanNumber)
                                    || d.VolumeNumber.Contains(searchInputModel.VolumeNumber)
                                    || d.FileUrl.Contains(searchInputModel.OwnersName));

                    }
                    Logger.Info($"Results found {result}");
                    return result;
                }
                return null;
            }

现在我唯一的问题是如何在视图中显示搜索结果。我如何做一个AttrID和ValStr匹配的foreach,如果用户只搜索一个字段(因为它是一个多搜索系统),我希望结果显示所有具有相同ID的字段(即d.ID)。感谢。

3 个答案:

答案 0 :(得分:1)

你能这样做吗?

foreach(var myProp in _entities.LLAttrDatas)
{
    if(myProp.AttrID == 1 && myProp.ValStr.StartsWith("foo"))
        // etc..
    else if (myProp.AttrID == 2 && myProp.ValStr.StartsWith("bar"))
        // etc..
}

答案 1 :(得分:0)

试试这个:

var results = _entities.LLAttrDatas.select(x = > new {
     owner = x.Where(n => n.AttrID == 2 && n.ValStr.Contains(searchInputModel.OwnersName)).FirstOrDefault(),
     ownerAddress = x.Where(n => n.AttrID == 3 && n.ValStr.Contains(searchInputModel.OwnersAddress)).FirstOrDefault(),
     propertyAddress = x.Where(n => n.AttrID == 4 && n.ValStr.Contains(searchInputModel.DescriptionOrLocationOfProp)).FirstOrDefault(),
     propertyVolumeNo = x.Where(n => n.AttrID == 5 && n.ValStr.Contains(searchInputModel.VolumeNumber)).FirstOrDefault(),
     propertyPlanNo = x.Where(n => n.AttrID == 6 && n.ValStr.Contains(searchInputModel.SurveyPlanNumber)).FirstOrDefault()
}).ToList();

答案 2 :(得分:0)

您的AttrID显然是搜索模型中属性的别名。正确定义别名表,您将提高生成请求的可读性和速度:

        var forwardMap = new Dictionary<int, string>()
                       {
                           {2, searchInputModel.OwnersName},
                           {3, searchInputModel.OwnersAddress},
                           {4, searchInputModel.DescriptionOrLocationOfProp},
                           {5, searchInputModel.VolumeNumber},
                           {6, searchInputModel.SurveyPlanNumber},
                       };

        //common request for all your properties. You can wrap it into function.
        var query = _entities.LLAttrDatas
                             .GroupBy(x => x.AttrID)
                             .Where(x => forwardMap.Keys.Contains(x.Key))
                             .Select(x => new
                                          {
                                              id = x.Key,
                                              val = x.FirstOrDefault(y => y.ValStr.Contains(forwardMap[x.Key]))
                                          });

        //request to server
        var result = query.ToDictionary(x => x.id, x => x.val);

        //now result contains corresponding values (don't forget to check them for existance, if they are not present in table, they will not be present in result and throw exception)
        ViewBag.searchName = result[2];
        ViewBag.searchAddress = result[3];
        ViewBag.searchProperty = result[4];
        ViewBag.searchPropertyVolumeNo = result[5];
        ViewBag.searchPropertyPlanNo = result[6];