Dynamic LINQ Multiple Where Clause

2019-06-07 14:59发布

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.

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.

4条回答
倾城 Initia
2楼-- · 2019-06-07 15:26

If the set of values to include/exclude is known at compile-time (as appears to be the case in your example), I think this can be managed with one query:

string memberid = HttpContext.Current.Session["MemberID"].ToString(); 
string inhouse = HttpContext.Current.Session["Inhouse"].ToString();
string supplier = HttpContext.Current.Session["Supplier"].ToString();

bool includeInHouse = (inhouse == "Inhouse");
bool includeSupplier = (supplier == "Supplier");

MyEnts autocomplete = new MyEnts();

var r = from p in autocomplete.tblAutoCompletes
            where (p.MemberId == memberid && p.LocationId == locationid && p.ACItem.Contains(prefixText))
            && (includeInHouse || (p.ACItem != "InHouse"))
            && (includeSupplier || (p.ACItem != "Supplier"))
            select p.ACItem;

r.OrderBy(p => p.ACItem);

return r.ToArray();

I've eliminated a couple cases for brevity.

查看更多
神经病院院长
3楼-- · 2019-06-07 15:26

Sorted.

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 != "Group");

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

        r = r.OrderBy(p => p);

        return r.ToArray();

I had to set the exception in quotation marks as the session vlaue was inappropriate and wouldn't have picked out anything from the list.

Thanks to all those contributing and helping me out.

查看更多
戒情不戒烟
4楼-- · 2019-06-07 15:30

Wouldn't each of your Where clauses just need to contain a Contains criteria and some Not Contains?

if (inhouse == "Inhouse")
     r = r.Where(p => p.Contains(inhouse) && !p.Contains("Supplier") && !p.Contains("Group") && !p.Contains("External"));
查看更多
Luminary・发光体
5楼-- · 2019-06-07 15:32

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);
查看更多
登录 后发表回答