繁体   English   中英

从C#或LINQ列表中过滤

[英]Filtering from list of C# or LINQ

我试图从attachList过滤taxheaderID,它来自我的数据库,其结构如此。

public int attachmentID { get; set; }
public int headerID { get; set; }
public string uploadedfilename { get; set; }
public string originalfilename { get; set; }
public string foldername { get; set; }

以下是从数据库获取数据的代码:

public JsonResult GetAllAttach()
{
    using (car_monitoringEntities contextObj = new car_monitoringEntities())
    {
        var attachList = contextObj.car_taxcomputationattachment.ToList();
        return Json(attachList, JsonRequestBehavior.AllowGet);
    }
}

这些是我的尝试:

attachList
    .Select(x => x.headerID)
    .Where(x => x == x)
    .Take(1);

和:

attachList = attachList
    .Where(al => attachList
        .Any(alx => al.taxheaderID == alx.headerID 
                 && al.headerID == alx.headerID));

问题是我想解析单个headerID上的多个附加或基于headerID过滤它们。 例如:

要修复的问题:

这是表格

期望的输出: 组合

数据表: 数据表 数据表2

这是获得输出的实际解决方案,但我的同事告诉我,这不是一个好习惯,这就是为什么我试图在函数本身中过滤它。 道歉,谢谢!

<div ng-repeat="att in attach|filter:{headerID:header.headerID}:true">

      <a href="~/UploadedFiles/{{att.uploadedfilename}}" download="{{att.uploadedfilename}}" target="_blank">{{att.uploadedfilename}} <br /></a>

 </div>

假设您有一个要在本地变量中过滤的标题ID,那么您几乎是正确的

int headerIdToFind = 19;
// think of x as a local variable inside a foreach loop which
// iterates over each item in the attachList (it does not exist 
// outside the where method)
// this is what you got wrong when you compared the item to itself
var filteredAttach = attachList.Where(x => x.headerId = headerIdToFind);

// if you want to select only some properties based on header id
// you can use select to project those properties
var filteredAttach = attachList.Where(x => x.headerId = headerIdToFind).
                      Select(x => new {x.attachmentId, x.folderName});

// based on last image, you only want to select (project) header id and the 
// filename. so you do not need where (filter) at all
// you can put all the properties you need in the select clause
var filteredAttach = attachList.Select(x => new {x.headerId, x.attachmentId});
// you can enumerate the filtered attach list of convert it into a list 
var filteredAttach = filteredAttach.ToList();

通过Id获取附件

public JsonResult GetAllAttach(int headerId)
{
    using (car_monitoringEntities contextObj = new car_monitoringEntities())
    {
        var attachList = contextObj.car_taxcomputationattachment
                                   .Where(x => x.headerID == headerId)
                                   .ToList();
        return Json(attachList, JsonRequestBehavior.AllowGet);
    }
}

如果要将所有数据放在一个JSON结果中,则需要创建嵌套视图模型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM