简体   繁体   中英

Filtering the result set of a LINQ query

I have the following LINQ query that works fine:

var comps = (from c in tc.companies                                 
                join r in tc.registry
                on c.Key equals r.Key
  select new { FieldValue=r.FieldValue, Name=c.Name, Company=c.Company,Industry=c.Industry,Rank=c.Rank});

Now I want the query to only return those records where FieldValue equals to the value submitted from TextBox1

I have tried:

var comps = (from c in tc.companies                                 
                join r in tc.registry
                on c.Key equals r.Key
        where r.FieldValue==TextBox1
  select new { FieldValue=r.FieldValue, Name=c.Name, Company=c.Company,Industry=c.Industry,Rank=c.Rank});
            return View(comps);

and

 var comps = (from c in tc.companies                                 
                join r in tc.registry
                on c.Key equals r.Key
        where r.FieldValue==TextBox1
  select new { FieldValue=r.FieldValue, Name=c.Name, Company=c.Company,Industry=c.Industry,Rank=c.Rank});
            comps=comps.Where(x => x.FieldValue== TextBox1);
            return View(comps);

But neither returns any data. What am I doing wrong?

Update:

public ActionResult Index(string TextBox1)
    {
        if (TextBox1 != null)
        {
            var comps = (from c in tc.companies                                 
                join r in tc.registry
                on c.Key equals r.Key
        where r.FieldValue==TextBox1
  select new { FieldValue=r.FieldValue, Name=c.Name, Company=c.Company,Industry=c.Industry,Rank=c.Rank});
            return View(comps);

        }
}

SOLVED! Answer is below! Not what I thought - reversing the table order in the query worked. Interesting pafr is that the query without a filter worked regardless of table order

The problem might be with what you think you are doing and what you actually do

You see as with most programing languages, in C# String is not value type but object, and the thing you are doing is actually comparing two adresses so to put it simply you check if the object in textBox1 is the same object you have as r.FildValue and what you really what to do is checking its contents, in C# each objeact has method Equals for comparing to other.

Try

where TextBox1.Equals(r.FieldValue)

The other think that you should check is if TextBox1 is value is correct

You can use System.Diagnostic.Debug.WriteLine("MyText" + TextBox1); to do that

Cheers :)

Hmmm, very strange. I was able to resolve the issue by switching the table order. Strangely the LINQ query worked fine when there was no filter but once filter was added - nothing. So I reversed the order of the tables and instead of

var comps = (from c in tc.companies                                 
            join r in tc.registry
            on c.Key equals r.Key
    where r.FieldValue==TextBox1
select new { FieldValue=r.FieldValue, Name=c.Name, Company=c.Company,Industry=c.Industry,Rank=c.Rank});
        return View(comps);

used:

var comps = (from r in tc.registry                                 
            join c in tc.companies 
             on r.Key equals c.Key
    where r.FieldValue==TextBox1
 select new { FieldValue=r.FieldValue, Name=c.Name, Company=c.Company,Industry=c.Industry,Rank=c.Rank});
        return View(comps);

and it worked like a charm!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM