Search code examples
c#linqnotinin-operator

In operator in LINQ query


There is several items in drop-down i.e.

Apply
Orange
Grapes
Factory

I try to fill drop down with two linq queries like this

        if (!Page.IsPostBack)
        {

            if (Session["UserName"] == "admin")
            {
                //List<spadminlist_Result> admin = tea.spadminlist().ToList();

                var admin=from ad in tea.tblReg
                          .Where((x=>x.Region).Contains("Factory"))
                          .GroupBy(x=>x.Region)
                          .Select(x=> new {Region=x.Key})
                          .ToList();



                regiondrop.DataSource = admin;
                regiondrop.DataTextField = "Region";
                regiondrop.DataValueField = "Region";
                regiondrop.DataBind();
                Label4.Visible = false;

            }
            else
            {
                var list = tea.tblReg.AsEnumerable()
                   .Where(x => !x.Region.Any(char.IsDigit) && (x.Region != ""))
                   .GroupBy(x => x.Region)
                   .Select(x => new { Region = x.Key, Value = x.Key })
                   .ToList();

                regiondrop.DataSource = list;
                regiondrop.DataTextField = "Region";
                regiondrop.DataValueField = "Region";
                regiondrop.DataBind();
                Label4.Visible = false;
            }

        }

now when i try this line //List admin = tea.spadminlist().ToList(); then datasource show null where as sp return values ie. Factory

so i decide to qrite linq query so after write linq query this

var admin=from ad in tea.tblReg
                          .Where((x=>x.Region).Contains("Factory"))
                          .GroupBy(x=>x.Region)
                          .Select(x=> new {Region=x.Key})
                          .ToList();

this shows an error

Error 3 A query body must end with a select clause or a group clause

and also i want if role is not admin then i dont want to display Factory value and if the role is admin then only i want to display value "Factory" in dropdown so how i use not in operator in 2nd linq query and for in operator i use contains operator in 1st LINQ but this show an error as i above mentioned


Solution

  • The syntax of your Where is wrong and you don't need to GroupBy.

    var admin = tea.tblReg.Where(x => x.Region == "Factory")
                          .Select(x => x.Region)
                          .Distinct()
                          .ToList();
    

    This will retrieve from database only the record where Region is "Factory" and will created it in the same form like in the else


    You can also refactor a bit your code to reduce the repetition (and more can still be done):

    if (!Page.IsPostBack)
    {
        if (Session["UserName"] == "admin")
        {
            regiondrop.DataSource = tea.tblReg.Where(x => x.Region == "Factory")
                                       .Select(x => x.Region)
                                       .Distinct()
                                       .ToList();
        }
        else
        {
            regiondrop.DataSource = tea.tblReg.Where(x => x.Region.All(char.IsLetter) && 
                                                          x.Region != "" &&
                                                          x.Region != "Factory")
                                              .Select(x => x.Region)
                                              .Distinct()
                                              .ToList();
        }
    
        regiondrop.DataTextField = "Region";
        regiondrop.DataValueField = "Region";
        regiondrop.DataBind();
        Label4.Visible = false;
    }
    

    Notice also that I changed the use of !Any(char.IsDigit) to All(char.IsLetter) - a clearer way of doing so