如何在列表中搜索<​​字符串>

时间:2018-11-11 11:45:20

标签: c# linq

我有一个List<string>,其中某些项目在我的视图中看起来像这样:

1S61A9MAT1DKV5N74925,
1S61A9MAT1DKV5X49329,
1S61A9MAT1DKV5X49857

,我想搜索此项目,但说实话,我不知道该告诉控制器,List<string>到时我在控制器s.SerialNoInvoiceOrdrelineDeliveryCloses.Contains(searchString)中尝试过类似的操作,但是不起作用,我的意思是当我搜索此项1S61A9MAT1DKV5N74925时,它可以将值传递给控制器​​,但找不到我搜索的该项的任何结果。正如您在我的
控制器中看到的那样,我有一些属性是字符串,并且有一个List<string>' SerialNoInvoiceOrdrelineDeliveryCloses '

控制器:

 public ActionResult test(string searchString) {

        string EmailID = Session["Email"].ToString();

        var v = (from cbr in db.Contact_Business_Relation
                 join c in db.Contact on cbr.Contact_No_ equals c.Company_No_
                 join sih in db.Sales_Invoice_Line on sa.No_ equals sih.Document_No_
                 where c.E_Mail == EmailID

                 select new ClosedOrders
                 {
                     OrderNumber = sa.Order_No_,
                     Fakturanummer = sih.Document_No_,
                     varnummer = sih.No_,

                     //List<string>
                     SerialNoInvoiceOrdrelineDeliveryCloses = db.Item_Ledger_Entry
                     .Where(s => s.Item_No_ == sih.No_)
                     .Select(s => s.Serial_No_)
                     .ToList()
                 });

        if (searchString !=null)
        {
            v = v.Where(s => s.OrderNumber.Contains(searchString) 
                        || s.Fakturanummer.Contains(searchString)
                        || s.varnummer.Contains(searchString) 
                        || s.SerialNoInvoiceOrdrelineDeliveryCloses.Contains(searchString) 
                      );
        }
        return View(v);
    }

ViewModel:

public class ClosedOrders
{
    public string OrderNumber { get; set; }
    public string Fakturanummer { get; set; }
    public List<string> SerialNoInvoiceOrdrelineDeliveryCloses { get; set; }
    public string varnummer { get; set; }
}

查看:

@using (Html.BeginForm("test", "Account", FormMethod.Get))
{
    <p>
        Find by name: @Html.TextBox("SearchString")
        <input type="submit" value="Search" />
    </p>
}

//Loop Model
        foreach (var item in Model)
        { 
            <tr>
                <td>
                    @item.Fakturanummer
                </td>
                <td>
                    @item.OrderNumber
                </td>
                <td> @item.varnummer</td>
                <td>
                    @String.Join(", ", item.SerialNoInvoiceOrdrelineDeliveryCloses)
                </td>
            </tr>
        }

2 个答案:

答案 0 :(得分:1)

直接答案可能是

// || s.SerialNoInvoiceOrdrelineDeliveryCloses.Contains(searchString) 
   || s.SerialNoInvoiceOrdrelineDeliveryCloses.Any(x => x.Contains(searchString)) 

答案 1 :(得分:0)

问题是您正在将v作为IQueryable<ClosedOrders>发送到视图。将IQueryable传递给视图并让其运行查询是一种不好的做法。首先,因为您要将未解决的查询发送到终点,其次,您需要向视图代码添加相关的引用。建议您在查询末尾添加.ToList()并将列表发送到视图:

var v = (from cbr in db.Contact_Business_Relation
                 join c in db.Contact on cbr.Contact_No_ equals c.Company_No_
                 join sih in db.Sales_Invoice_Line on sa.No_ equals sih.Document_No_
                 where c.E_Mail == EmailID

                 select new ClosedOrders
                 {
                     OrderNumber = sa.Order_No_,
                     Fakturanummer = sih.Document_No_,
                     varnummer = sih.No_,

                     //List<string>
                     SerialNoInvoiceOrdrelineDeliveryCloses = db.Item_Ledger_Entry
                     .Where(s => s.Item_No_ == sih.No_)
                     .Select(s => s.Serial_No_)
                     .ToList()
                 }).ToList();

和:

v = v.Where(s => s.OrderNumber.Contains(searchString) 
                        || s.Fakturanummer.Contains(searchString)
                        || s.varnummer.Contains(searchString) 
                        || s.SerialNoInvoiceOrdrelineDeliveryCloses.Contains(searchString) 
                      ).ToList();