Linq To Entities-Anonymous types error...

I get a syntax error on the .ToList() object when I try to reference a anonymous  new field: mycompname,
the error is cannot  implicity convert type system.collection.generic.list<anonymoustype#1>
to system.collection.generic.list<string>

 List<string> wow = new List<string>();
        using (var dc = new MyEntities())
        {
                 wow = (from p in dc.mytable
                  select new { mycompname = p.COMPANYName }).ToList();                  ;
        }
7of9121098Asked:
Who is Participating?
 
Snarf0001Connect With a Mentor Commented:
Using new {} creates an anonymous type, just select the member directly.


 List<string> wow = new List<string>();
        using (var dc = new MyEntities())
        {
                 wow = (from p in dc.mytable
                  select p.COMPANYName).ToList();
        }
0
 
7of9121098Author Commented:
I need to create as a anonymous type because I want to assign it to a user defined function. I got this far.  (In SQL server the user defined function returns a varchar and inputs are varchar and int-2 input values). Therefore I declared them as a string type in the stud function.
therefore this is the error I get.
  The specified method 'System.String fComArray(System.String, Int32)' on the type 'MyFunctions' cannot be translated into a LINQ to Entities store expression.

public static class MyFunctions
 {
   [EdmFunction("MyEntityModel.MyEntity", "fComArray")]
   public static string fComArray(string Companyname, int NumOfYears)
     {throw new NotSupportedException();}
 }  

public class MyResultingType
{
  public string mycompanyname { get; set; }
  public string UDFValue {get;set;}
}

 using (var dc = new MyEntities())
     {
          var items = from p in dc.MyTable select new MyResultingType()
          {   mycompanyname = p.COMPANYNAME, UDFValue= MyFunctions.fComArray  (p.COMPANYNAME,4)};
           
      // I get the error here...as listed above at run-time.
        foreach (var prime in items)
          { Response.Write(prime.COMPANYNAME + "<br>"); }  
      }

0
 
7of9121098Author Commented:
I found the solution is was a problem with the name in the Edm function. Thanks for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.