none
Annonymouse Type to array of ints RRS feed

  • Question

  • I want to do something like this.  Or at least I want the end result to be an array of Ints.

    int
    [] j = (from t in this.Context.CustomerProjects

    where t.projectID == id

    select new { t.projectID }).ToArray();



    ****************************************************

    My true goal is to be able to later reuse the data in this.

    public object GetEquipment(int[] ids)

    {object q =

    from t in Context.Equipments

    join ean in Context.zlkEquipmentAttributeNames on t.attributeNameID equals ean.attributeNameID

    join p in Context.CustomerProjects on t.projectID equals p.projectID

    where ids.Contains(p.projectID)

    select new { ProjectNumber = p.projectNumber, Description = ean.description, Model = ean.modelNumber, equipmentID = t.equipmentID, attributeID = t.attributeNameID };

    return q;

    }


    joe alt
    Thursday, March 12, 2009 1:42 PM

Answers

  • You don't need the anonymous type at all. Without it, the query results in the type IQueryable<int> which easily converts to int[] via ToArray().

    int[] j = (from t in this.Context.CustomerProjects
                 where t.projectID == id
                 select t.projectID).ToArray();

    Of course, you probably want a different where clause.

    Wayward LINQ Lacky
    • Marked as answer by altja Thursday, March 12, 2009 5:52 PM
    Thursday, March 12, 2009 3:35 PM
    Moderator

All replies

  • You don't need the anonymous type at all. Without it, the query results in the type IQueryable<int> which easily converts to int[] via ToArray().

    int[] j = (from t in this.Context.CustomerProjects
                 where t.projectID == id
                 select t.projectID).ToArray();

    Of course, you probably want a different where clause.

    Wayward LINQ Lacky
    • Marked as answer by altja Thursday, March 12, 2009 5:52 PM
    Thursday, March 12, 2009 3:35 PM
    Moderator
  • Thanks that was very helpful.
    joe alt
    Thursday, March 12, 2009 5:53 PM