none
Return collection from query...not working. why? RRS feed

  • Question

  • Code Snippet

    var clients = from c in _db.Clients
    from o in c.ClientAddresses
    where c.ClientName.Contains(criteria) ||
    o.Line1.Contains(criteria)
    select new { c, o };


    Above is what I am trying to accomplish(joining tables), put I don't know how to use that in a Public return method. It won't let me. What object can I use to return the data?

    Thank you for your time. LINQ to SQL is the best, just trying to learn it better Smile
    Thursday, August 21, 2008 5:37 PM

Answers

  • Hi Brenda,

    I ran in to the same problem in my first hour or so of using LINQ. I use the generic form of IQueryable whenever I am returning a full table. So where I am returning customer I would use IQueryable<Customer>. If I am using shaping and only using a subset of columns or columns from more than one table I just return IQueryable as below for your scenario:

    public IQueryable MyMethod()
    {
    var clients = from c in _db.Clients
    from o in c.ClientAddresses
    where c.ClientName.Contains(criteria) ||
    o.Line1.Contains(criteria)
    select new { c, o };

    return var;
    }

    This does have the disadvantage that what you return is no longer strongly typed but is fine if you just want to set the datasource on say a gridview.

    Hope this helps.

    Tuesday, August 26, 2008 8:23 PM

All replies

  • The line "select new { c, o };" is the problem - this is an anonymous type and anonymous types can not be returned from methods.

    Normally you would define a new type and create instances of that returning an IEnumerable<NewType> but in your scenario I don't think this is required.

    If you create an association between Clients and Addresses then you would not need to do the join as it would be handled for you and also when you return instances of Client you would be able to navigate the association to get the address related to it.

    [)amien
    Thursday, August 21, 2008 7:12 PM
    Moderator
  • Amien

    Will you show me an example of the
    association between Clients and Addresses?  I already have a foreign key. Yes, I don't want to have to create a bunch of NewTypes for queries like this.  Will you show me some sample code? THANKS!
    Thursday, August 21, 2008 7:18 PM
  • Something like this should do it:

    public IEnumerable<Client> GetClients(string criteria)
    {
    return from c in _db.Clients
    where c.ClientName.Contains(criteria) ||
    c.Address.Line1.Contains(criteria)
    select c;

    }

    Then to access the address parts in the calling code you would just access the elements via the Address property.

    [)amien
    Thursday, August 21, 2008 9:50 PM
    Moderator
  • Damien

    I have a separate address table so Clients can have multiple addresses so I can't do this cuz there is no such thing.

    c.Address.Line1.Contains(criteria)

    How do I handle that situation?
    Thursday, August 21, 2008 9:59 PM
  • What are you expecting the first sample of code to return if it were to work?

     

    I see this as you are returning either a customer... or an address... or both?

     

    [)amien

     

    Friday, August 22, 2008 10:55 PM
    Moderator
  • I want it to return a List of Clients, but I need it to search the Client and ClientAddress table at the same time. Does that make sense?

    Thanks!
    Saturday, August 23, 2008 4:01 AM
  • Or can I do something like this?

    from c in _db.Clients
                       from ca in _db.ClientAddresses
                       where c.ClientName.Contains(criteria) ||
                       ca.Line1.Contains(criteria)
                       select new { c.ClientID, ca.Line1 };

    And return that new object? How do I return it? What do I use? Thanks!
    Tuesday, August 26, 2008 6:18 PM
  • If you just want the customer object and the ability to navigate through the addresses then just "select c" and mark the return type as IQueryable<Client>.

     

    [)amien

     

    Tuesday, August 26, 2008 7:19 PM
    Moderator
  • Hi Brenda,

    I ran in to the same problem in my first hour or so of using LINQ. I use the generic form of IQueryable whenever I am returning a full table. So where I am returning customer I would use IQueryable<Customer>. If I am using shaping and only using a subset of columns or columns from more than one table I just return IQueryable as below for your scenario:

    public IQueryable MyMethod()
    {
    var clients = from c in _db.Clients
    from o in c.ClientAddresses
    where c.ClientName.Contains(criteria) ||
    o.Line1.Contains(criteria)
    select new { c, o };

    return var;
    }

    This does have the disadvantage that what you return is no longer strongly typed but is fine if you just want to set the datasource on say a gridview.

    Hope this helps.

    Tuesday, August 26, 2008 8:23 PM