Filtering from list of C# or LINQ

时间:2019-04-17 01:41:31

标签: c# linq lambda model

I am trying to filter from attachList the taxheaderID, it comes from my database which is structured as such.

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; }

Here is the code that gets data from the database:

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

These are my attempts:

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

and:

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

The problem is I want to parse multiple attach on a single headerID or filter them base on headerID. For example:

Problem to fix:

This is the table

Desired output: Combined

data table: data table data table 2

Here is the actual solution that was made to get the output, but my coworker told me that it is not a good practice that's why I'm trying to filter it in the function itself. apologies for the trouble, thanks!

<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>

2 个答案:

答案 0 :(得分:0)

Assuming you have the header id on which you want to filter in a local variable, you are almost correct

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();

答案 1 :(得分:0)

To get attachments by 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);
    }
}

If you want to have all data in one JSON result, then you need to create a nested view model.