score:6

Accepted answer

You cannot* return an anonymous type because the caller would not know what type it is and wouldn't be able to use it.

If you want to return the results, you can create objects of a non-anonymous type:

IEnumerable<Foo> assets =
    from Product p in Session.CreateLinq<Product>()
    where bundles.Contains(p.ProductBundle)
    select new Foo { Bar = p.Asset, Baz = p.Asset.PropertyTbl};

You can also use the Tuple type in .NET 4 if you don't want to create a custom class for your values.


* This is not strictly true - it is possible but you should avoid doing it. Here is a link anyway if you really want to.

score:0

Not really. If you cast to object you wont be able to access the properties of your anonymous class.

The var keyword was specifically introduced for dealing with anonymous classes - why would you want to avoid it? If you need to return the data you should name the class.

score:0

Not really, since the new {p.Asset, p.Asset.PropertyTbl} code creates an anonymous type. Even using object doesn't really gain you much since you can't cast it to anything useful later on, so you would have to use reflection to access the properties.

score:0

You can if you use lambda expressions, otherwise you can do a cast but do some good exception handling.

score:0

you can also do this (it does relate much to your problem though, because you just move "var" somewhere else, but it's interesting that it recognize those types as same)

        var element = new { id = 7 };

        List<object> collection = new List<object>();
        element = collection.Select(item => new { id = 0 }).First();

score:1

You could define a new class:

public class AssetProp
{
   public virtual string Asset {get;set;}
   public virtual string PropertyTbl {get;set;}
}

And then you can return it as that class:

IEnumerable<AssetProp> assets =
    from Product p in Session.CreateLinq<Product>()
    where bundles.Contains(p.ProductBundle)
    select new AssetProp {p.Asset, p.Asset.PropertyTbl};

score:4

You can use object or dynamic (in .NET 4.0) instead of var but don't expect to find a name to an anonymous type. In your case using var is better as it will preserve the strong typing at least until you leave the scope of the current method.


Related Query

More Query from same tag