Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.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# 使用「;包括「;匹配lambda表达式中字符串的一部分_C#_Jquery_Asp.net Mvc 4_Lambda - Fatal编程技术网

C# 使用「;包括「;匹配lambda表达式中字符串的一部分

C# 使用「;包括「;匹配lambda表达式中字符串的一部分,c#,jquery,asp.net-mvc-4,lambda,C#,Jquery,Asp.net Mvc 4,Lambda,我有一个MVC页面,其中有三个不同的“输入”元素,都是同一个类,使用jQuery自动完成。对于其中任何一个,我都在控制器中执行此操作: [HttpGet] public ActionResult GetAllItemsEdit(string data, string source) { List<TextValuePair> items = InventoryControlRepository.GetAllItemsTVP(); var result1 = items

我有一个MVC页面,其中有三个不同的“输入”元素,都是同一个类,使用jQuery自动完成。对于其中任何一个,我都在控制器中执行此操作:

[HttpGet]
public ActionResult GetAllItemsEdit(string data, string source)
{
    List<TextValuePair> items = InventoryControlRepository.GetAllItemsTVP();
    var result1 = items.Where(item => item.Text.Contains(data.ToUpper())).ToList();
    return Json(result1, JsonRequestBehavior.AllowGet);
}
因此,TVP的
text
部分中的文本必须根据源输入字段进行匹配。你知道怎么做吗?我需要以某种方式拆分
item.Text
字段,并检查基本上三列数据中的一列,以匹配页面的输入

多亏了皮特的回答,我才得以完成。我必须向模型中添加一个字段,本质上是一个搜索字段,其中包含我想要的文本字段值

 [HttpGet]
    public ActionResult GetAllItemsEdit(string data, string source)
    {
        IEnumerable<ItemModel> models = InventoryControlRepository.GetAllItems();
        switch (source)
        {
            case "txtFindSerial":
                models = models.Where(x => x.SerialNumber.IndexOf(data, StringComparison.InvariantCultureIgnoreCase) > -1);
                break;
            case "txtFindItem":
                models = models.Where(x => x.ItemNumber.IndexOf(data, StringComparison.InvariantCultureIgnoreCase) > -1);
                break;
            case "txtFindDescription":
                models = models.Where(x => x.ItemDescription.IndexOf(data, StringComparison.InvariantCultureIgnoreCase) > -1);
                break;
        }

        var result1 = models.Select(item => new TextValuePair() { Text = item.SearchField, Value = item.ItemNumber }).ToList();
        return Json(result1, JsonRequestBehavior.AllowGet);
    }
[HttpGet]
public ActionResult GetAllItemsEdit(字符串数据,字符串源)
{
IEnumerable models=InventoryControlRepository.GetAllItems();
开关(源)
{
案例“txtFindSerial”:
models=models.Where(x=>x.SerialNumber.IndexOf(data,StringComparison.InvariantCultureIgnoreCase)>-1);
打破
案例“txtFindItem”:
models=models.Where(x=>x.ItemNumber.IndexOf(data,StringComparison.InvariantCultureIgnoreCase)>-1);
打破
案例“txtFindDescription”:
models=models.Where(x=>x.itemsdescription.IndexOf(data,StringComparison.InvariantCultureIgnoreCase)>-1);
打破
}
var result1=models.Select(item=>newtextValuePair(){Text=item.SearchField,Value=item.ItemNumber}).ToList();
返回Json(result1,JsonRequestBehavior.AllowGet);
}

我会搜索您的初始项目(在您将其放入
TextValuePair
列表之前),然后您可以执行以下操作

IEnumerable<Item> items = originalItemsList;
switch (source)
{
    case "1": // or whatever this should be
        items = items.Where(x => x.ItemNumber.IndexOf(data, StringComparison.InvariantCultureIgnoreCase) > -1);
        break;

    case "2": // or whatever this should be
        items = items.Where(x => x.ItemDescription.IndexOf(data, StringComparison.InvariantCultureIgnoreCase) > -1);
        break;

    case "3": // or whatever this should be
        items = items.Where(x => x.SerialNumber.IndexOf(data, StringComparison.InvariantCultureIgnoreCase) > -1);
        break;
}


var result1 = items.Select(item => new TextValuePair() { text = item.Text, Value = item.ItemNumber }).ToList();

return Json(result1, JsonRequestBehavior.AllowGet);
然后有一个方法:

private static bool TextMatches(string text, string data, string source)
{
    // you may want to chose a better delimiter if your text description contains a " - "
    string[] textParts = text.Split(new string[] { " - " }, StringSplitOptions.None);

    switch (source)
    {
        case "1": // or whatever this should be
            return textParts[0].IndexOf(data, StringComparison.InvariantCultureIgnoreCase) > -1;

        case "2": // or whatever this should be
            return textParts[1].IndexOf(data, StringComparison.InvariantCultureIgnoreCase) > -1;

        case "3": // or whatever this should be
            return textParts[2].IndexOf(data, StringComparison.InvariantCultureIgnoreCase) > -1;
    }

    return false;
}
var result1 = items.Where(item => TextMatches(item.Text, data, source)).ToList();
private static bool TextMatches(string text, string data, string source)
{
    // you may want to chose a better delimiter if your text description contains a " - "
    string[] textParts = text.Split(new string[] { " - " }, StringSplitOptions.None);

    switch (source)
    {
        case "1": // or whatever this should be
            return textParts[0].IndexOf(data, StringComparison.InvariantCultureIgnoreCase) > -1;

        case "2": // or whatever this should be
            return textParts[1].IndexOf(data, StringComparison.InvariantCultureIgnoreCase) > -1;

        case "3": // or whatever this should be
            return textParts[2].IndexOf(data, StringComparison.InvariantCultureIgnoreCase) > -1;
    }

    return false;
}