none
Just need a couple of fields RRS feed

  • Question

  • So I have this:
                    Agents = from a in db.PRODUCERs
                             where a.Tax_ID.Contains(txtTaxID.Text.Trim().Normalize())
                             select a;

    But I only need Tax_ID and Name out of the db.PRODUCERs table.  How do I do this?
    Wednesday, September 10, 2008 4:24 PM

Answers

  •  

    You can change this to:

     

    var Agents = from a in db.PRODUCERs
                             where a.Tax_ID.Contains(txtTaxID.Text.Trim().Normalize())
                             select new { a.Tax_ID, a.Name };

    Which creates an anonymous type on-the-fly with just those two fields.

     

    [)amien

    Wednesday, September 10, 2008 4:59 PM
    Moderator

All replies

  •  

    You can change this to:

     

    var Agents = from a in db.PRODUCERs
                             where a.Tax_ID.Contains(txtTaxID.Text.Trim().Normalize())
                             select new { a.Tax_ID, a.Name };

    Which creates an anonymous type on-the-fly with just those two fields.

     

    [)amien

    Wednesday, September 10, 2008 4:59 PM
    Moderator
  • That's what I thought.  Doesnt work. Sad But not because the suggestion is wrong.  Smile

     

     

    Turns out I'm defining var Agents prior to the code I pasted in. Here's the correct (working) code.  Thanks!

     

     

    Code Snippet

    var Agents = from a in db.PRODUCERs

    where a.Tax_ID.Equals(txtTaxID.Text.Trim().Normalize())

    orderby a.Tax_ID

    select new { a.Tax_ID, a.Name };

    // if exact match didn't work

    if (Agents.Count() < 1)

    {

    Agents = from a in db.PRODUCERs

    where a.Tax_ID.Contains(txtTaxID.Text.Trim().Normalize())

    orderby a.Tax_ID

    select new { a.Tax_ID, a.Name };

    }

     

     

    Thursday, September 11, 2008 6:02 PM