Link to home
Create AccountLog in
Avatar of 7of9121098
7of9121098

asked on

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();                  ;
        }
ASKER CERTIFIED SOLUTION
Avatar of Snarf0001
Snarf0001
Flag of Canada image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of 7of9121098
7of9121098

ASKER

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>"); }  
      }

I found the solution is was a problem with the name in the Edm function. Thanks for your help.