使用带有c#后端的asp.net中的linq搜索关键字

时间:2015-05-09 11:17:26

标签: c# asp.net linq

    List<search> alllist = wsWSemloyee.GetAllProject(); //where search is model class contains properties..
    string search_key = "%" + txtsearch.Text.Trim() + "%";


    List<search> result = new List<search>();
    foreach (search item in alllist)
    {
     var op = (
                     from a in alllist
                     where a.Sfirstname.Contains(search_key) || a.Slastname.Contains(search_key) || a.Smob.Contains(search_key) || a.Scity.Contains(search_key) || a.Sstate.Contains(search_key)
                     //where SqlMethods.Like(a.Sfirstname,search_key)||SqlMethods.Like(a.Slastname,search_key)||SqlMethods.Like(a.Scity,search_key)||SqlMethods.Like(a.Smob,search_key)||SqlMethods.Like(a.Sstate,search_key)
                     select a
                   );

   //  List<search> lst = op.ToList<search>();
        if (op != null)
        {
            result.Add(item);
        }
    }


    if (result.Count != 0)
    {
        dgv_searchreport.DataSource = result;
        dgv_searchreport.DataBind();// data grid view
    }

它不起作用...... 给出alllist中的所有结果.. //其中搜索是模型类包含属性..

2 个答案:

答案 0 :(得分:2)

我是因为您正在比较您的linq查询的结果是否为null然后从foreach子句添加变量。当allproducts中的任何单个项目与条件匹配时,op将永远不为null,然后整个集合将包含在result中。你想要的可能是:

var result = (from a in alllist
              where a.Sfirstname.Contains(search_key) 
                    || a.Slastname.Contains(search_key) 
                    || a.Smob.Contains(search_key) 
                    || a.Scity.Contains(search_key) 
                    || a.Sstate.Contains(search_key)
              select a).ToList();

这将选择所有匹配条件的项目并枚举它们列出。

答案 1 :(得分:1)

这可以帮助你..

/vendor/firmware/ $ ls
bcm20795_firmware.ncd
bcm4350b1.hcd
bcm4350c0.hcd
fw_bcmdhd.bin
fw_bcmdhd_apsta.bin
maxim_fp35.bin
nvavp_aacdec_ucode.bin
nvavp_aud_ucode.bin
nvavp_mp3dec_ucode.bin
nvavp_os_0ff00000.bin
nvavp_os_8ff00000.bin
nvavp_os_eff00000.bin
nvavp_os_f7e00000.bin
nvavp_vid_ucode_alt.bin
rt5677_elf_vad
sar0_CY8C.img
sar1_CY8C.img
sensor_hub.img
synaptics.img
tegra13x
touch_fusion.cfg