none
how to get a "joined string" with separator from rows of a joined entity? ;) RRS feed

  • Question

  • Hello

     

    I'm looking for a quick way to get a "joined string" from a linq entity.

    Here my example :

     

    I've a Product table :

    ProductID, Title, Price, ...

     

    and a ProductProperty table :

    ID, ProductID, Name, ...

     

    Let's imagine the product : ProductID=3, Title=Shirt, Price=30, ...

    and the 2 properties : ID=14, ProductID=3, Name=Size, ... and ID=15, ProductID=3, Name=Color, ...

     

    Code Snippet

    MyDataContext db = new MyDataContext();

    var q = from p in db.Product

    [where clauses...]

    select new

    {

    p.ProductID,

    p.Title,

    p.....,

     

    [HERE I want to get a string "Properties" which give all the corresponding properties joined with a separator]

    Properties= ??

    };

     

     

     

    For the Product of ID 3, 'Properties' should be : "Size,Color"

     

    Thx

     

    Friday, November 14, 2008 1:42 PM

Answers

  • Hi,

     

    How's this:

     

    Code Snippet

    MyDataContext db = new MyDataContext();

    var q =

    from p in db.Product

    join pp in db.ProductProperty

    on p.ProductID equals pp.ProductID

    into prodsAndProdProps

    [where clauses...]

    select new

    {

    p.ProductID,

    p.Title,

    p.....,

     

    [HERE I want to get a string "Properties" which give all the corresponding properties joined with a separator]

    Properties=

    string.Join(",",

    (from prodProp in prodsAndProdProps

     select prodProp.Name).ToArray())

    };

     

    Joe

    Sunday, November 16, 2008 2:04 AM
    Answerer

All replies

  • Hi,

     

    How's this:

     

    Code Snippet

    MyDataContext db = new MyDataContext();

    var q =

    from p in db.Product

    join pp in db.ProductProperty

    on p.ProductID equals pp.ProductID

    into prodsAndProdProps

    [where clauses...]

    select new

    {

    p.ProductID,

    p.Title,

    p.....,

     

    [HERE I want to get a string "Properties" which give all the corresponding properties joined with a separator]

    Properties=

    string.Join(",",

    (from prodProp in prodsAndProdProps

     select prodProp.Name).ToArray())

    };

     

    Joe

    Sunday, November 16, 2008 2:04 AM
    Answerer
  •  

    Monday, November 17, 2008 1:09 PM