Linq to sql using new inner class file

I usually use linq to sql with IQueryable or IEnumerable to get data like this:

    public static IQueryable getSomething() 
    {
        var getit = from d in database.table select d;

        return getit;
    }

      

and it works great, but I'm trying to use select new with it like this:

    public static IQueryable getSomething() 
    {
       var getit = from d in database.table select new 
       { 
          value1 = d.1,
          value2 = d.2
       };

    return getit;
}

      

this is sample code, not actual code.

but it doesn't work, how to do it?

thank

-1


source to share


1 answer


You cannot inject any method in C # for the explicit type of anonymous types. They cannot be "named" so to speak, and therefore cannot appear in metadata signatures.

fooobar.com/questions/63963 / ...

First, you need to create a class like



public class MyClass
    {

        public string Property1{get;set;}
        public string Property2{ get; set; }
    }

      

Then replace your method,

public static IEnumerable<MyClass> getSomething() 
        {
           var getit = from d in database.table select new 
          MyClass  { 
              Property1 = d.1,
              Property2 = d.2
                   };

        return getit;
    }

      

+1


source







All Articles