Initializing strongly typed objects in LINQ to Entities

前端 未结 5 1479
别跟我提以往
别跟我提以往 2020-12-16 01:40

I have a plain old CLR object which is essentially a wrapper for two entity framework objects, I\'m doing this so I can pass this wrapper object to a strongly typed view in

5条回答
  •  旧时难觅i
    2020-12-16 01:56

    IF you add a parameterless constructor to your FooWrapper and then use object initialization instead, like so:

    public IEnumerable ListFoosWithBars(int userID)
    {
        IEnumerable tempBar = ListBarsByUserID(userID);
    
        IEnumerable results = (
            from f in _entities.FooSet
            join b in tempBar on f.ID equals b.foos.ID
            select new FooWrapper()
            {
                FooObject = f, 
                BarObject = b
            });
    
        return results;
    }
    

提交回复
热议问题