Set anonymous type to null

I know that it is not allowed to set a non-empty type to null, but how to solve it:

var products = null; //this cant be null, but somehow it must be declared in this outer scope, and not only inside the try-catch scope

    try
     {
         products = (from p in repository.Products
                     select new { p.Product, p.ProductName }).ToList();
     }
     catch (Exception e)
     {  
       return;
     }
Console.WriteLine(products.FirstOrDefault().ProductName)
+3
source share
7 answers

I agree with the other answers that you should consider reorganizing this code or using a nominal type rather than an anonymous type.

However, there is a way to get a null reference in an anonymous type variable. It is easy.

static List<T> GimmeANullListOf<T>(T t) { return (List<T>)null; }
...
var products = GimmeANullListOf(new { X = 1, Y = "hello" });

This trick is called an "example", and it is strange, but legal.

+11
source

Or new Nullable<bool>()easily complete the task.

+5
source

- , return , try , products :

try
{
    var products = (from p in repository.Products
                    select new { p.Product, p.ProductName }).ToList();
    Console.WriteLine(products.FirstOrDefault().ProductName);
}
catch (Exception e)
{  
    return;
}

SLaks, .

+2

. - , try{} ( , , ) ,

class ProductWithName
{
    public int Product;
    public string ProductName;
}

List<ProductWithName> products = null;

select new ProductWithName { Product = p.Product, ProductName = p.ProductName }

, .

+2

, , IEnumerable<T> ( IQueryable), , .

products ?? Enumerable.Empty<Product>() IEnumerable<T>.

. Exception , .

+1

.

:

IEnumerable<object> products = null;

// ...

var anon = products.FirstOrDefault();
Console.WriteLine(anon.GetType().GetProperty("ProductName").GetValue(anon, null));

:

IEnumerable<dynamic> products = null;

// ...

var anon = products.FirstOrDefault();
Console.WriteLine(anon.ProductName);
+1

LINQ , try catch :

var productsDB = new List<Func<string>>() { () => "Apples", () => "Pears", () => "Bannanas" };//, () => { throw new NotImplementedException(); } }; // sorry, couldn't think of a better way to make this fail in 2 mins..

var products = (
    from p in productsDB
    select new 
    {
        Name = p()
    } );

try
{
    products.ToList(); // runs the LINQ query
    products.Dump();   // prints the results (LINQPad)
}
catch { "there was an error".Dump(); }

- Tuple, :

var productsDB = new[] { "Apples", "Pears",  "Bannanas" };

List<Tuple<string>> products;

try
{
    products = (
        from p in productsDB
        select Tuple.Create( p ) ).ToList();

    products.Dump();
}
catch { "there was an error".Dump(); }

// Build anonymous type from (read-only) Tuple here if necessary...

: , : p - heres my original post:

, , - .

var employees = (
    from person in db.denormalisedPeople
    where person.Type == "employee"
    select new
    {
        name = employee.FullName,
        areaID = new Nullable<int>(), // placeholder.. set in a future query                
    } ).ToList();

( , )

0

Source: https://habr.com/ru/post/1787288/


All Articles