0

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

Coding Duchess
  • 6,445
  • 20
  • 113
  • 209
  • 4
    Maybe you should be comparing `r.FieldValue` to `TextBox1.Text`? `TextBox1` is the whole control, not the value within the textbox. `TextBox1.Text` is the property that contains the value entered into the textbox. – squillman Oct 06 '15 at 20:06
  • TextBox1 is not the whole control but a string value - I pass it to the Index Action of my controller – Coding Duchess Oct 06 '15 at 20:28
  • As a matter of good convention, using "==" for string comparison is generally [frowned upon](http://stackoverflow.com/questions/814878/c-sharp-difference-between-and-equals). The preferred method is String.Equals. That *might* be causing your issue... – bvoyelr Oct 06 '15 at 20:30
  • This is really hard to answer without seeing your data and the exact values you're comparing. Could be whitespace. Could be casing. Could be invisible characters. Could be something else entirely. Post a reproducible example with data included, or do some debugging and compare the value of `TextBox` with what is contained in the query object to see why it's not returning any values based on your filter. – Michael McGriff Oct 06 '15 at 20:47
  • What is the type of FieldValue? When it is object, you end up comparing references instead of values. So either change the type of it to string, or use .Equals(). – Niels V Oct 06 '15 at 21:20

2 Answers2

0

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 :)

Inverce
  • 1,487
  • 13
  • 27
  • 1
    `==` is identical to `.Equals()` for strings. C# is not java. – recursive Oct 06 '15 at 20:38
  • 1
    Yes and no. Comparing strings with == does make things an awful lot simpler and more readable but you need to remember that both sides of the operator must be expressions of type string in order to get the comparison to work properly. (Source: http://blogs.msdn.com/b/csharpfaq/archive/2004/03/29/when-should-i-use-and-when-should-i-use-equals.aspx) – Niels V Oct 06 '15 at 21:15
  • the value in TextBox1 is fine. I even tried hardcoding it as "where r.FieldValue=="00704050" and it did not produce any results even though resulting sql query gets the data – Coding Duchess Oct 07 '15 at 12:33
0

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!

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Coding Duchess
  • 6,445
  • 20
  • 113
  • 209