动态LINQ多个Where子句 [英] Dynamic LINQ Multiple Where Clause

查看:211
本文介绍了动态LINQ多个Where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不过真正与这种挣扎,似乎在圈子一轮去。

Still really struggling with this and appear to be going round in circles.

我有以下的代码,我发疯。它应该填充自动完成文本框使用的项目清单:

I have the following code that is driving me nuts. It should populate a list of items to be used in an autocomplete text box:

public string[] GetAutoComplete(string prefixText, int count)
    {
            string memberid = HttpContext.Current.Session["MemberID"].ToString(); 
            string locationid = HttpContext.Current.Session["LocationID"].ToString();
            string inhouse = HttpContext.Current.Session["Inhouse"].ToString();
            string supplier = HttpContext.Current.Session["Supplier"].ToString();
            string groupw = HttpContext.Current.Session["Group"].ToString();
            string external = HttpContext.Current.Session["External"].ToString();

            MyEnts autocomplete = new MyEnts();

            var r = from p in autocomplete.tblAutoCompletes
                        where p.MemberId == memberid && p.LocationId == locationid && p.ACItem.Contains(prefixText)
                        select p.ACItem;

            if (inhouse == "Inhouse")
                r = r.Where(p => p == inhouse);

            if (supplier == "Supplier")
                r = r.Where(p => p == supplier);

            if (groupw == "Group")
                r = r.Where(p => p == groupw);

            if (external == "External")
                r = r.Where(p => p == external);

            r.OrderBy(p => p);

            return r.ToArray();



我试图检索与动态那里沿着以下的线路条款。

What I am trying to retrieve with the dynamic where clause along the lines of the following.

室内应该=室内,那么项目的清单应包括单词室内。如果室内!=室内,室内这个词应该被排除在名单。

Should inhouse = "Inhouse", then the list of items should include the word "Inhouse". If inhouse != "Inhouse", the word "Inhouse" should be excluded from the list.

这同样的逻辑然后应在不同的where子句,即供应商应用,集团外部。

This same logic should then be applied across the different where clauses i.e. Supplier, Group, External.

我真的曾尝试很多不同的方法,但我不能为我的生命得到的东西工作,这有点令人沮丧的我。

I genuinely have tried lots of different methods but I cannot for the life of me get the thing to work and it's frustrating me somewhat.

如果任何人都可以提出这样做​​的方式,你要么得到一个大大的吻或一个大冷若冰霜的啤酒应我们的路永远交叉。

If anyone can suggest a way of doing this, you will either get a big kiss or a big frosty beer should our paths ever cross.

推荐答案

不完全知道你的问题在这里,但如果你想排除则不宜代码是这样的。

Not exactly sure about your problem here but if you want to exclude then shouldn't the code be something like

 if (inhouse == "Inhouse")
                r = r.Where(p => p == inhouse);
 else
                r = r.Where(p => p != inhouse);



哦!如果你只想排除则代码应该是这样

Oh! if you want just exclusion then the code should be something like

if (inhouse != "Inhouse")
                    r = r.Where(p => p != inhouse);

这篇关于动态LINQ多个Where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆