Click here to Skip to main content
15,885,027 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Dear All

I have a query that works well (in LINQPap), but when is running in my project I recive an error message.

This is the class

public class cl_ELAB
{

    public int id_pro{ get; set; }
    public string c_pro { get; set; }
    public int id_reg { get; set; }
    public int id_per { get; set; }
    public string c_per { get; set; }
    public string c_email { get; set; }
}


This is the message:
Error	CS0029	Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type: int id_pro, string c_pro, int id_reg, int id_per, string c_per, string c_email>>' to 'System.Collections.Generic.List<TAP_DAL.Dal.Dal_Tap_pro.cl_ELAB>'


Please let me know where i am going wrong.

What I have tried:

and this is the code

TAP_Entities ctx = CurrentDbContext;

List<cl_ELAB> aquery = new List<cl_ELAB>();

aquery = (from p in ctx.ty9_prog.Where(t => t.id_pro == 1234)
          join r in ctx.t11_reg on p.id_pro equals r.id_pro
          join s in ctx.t35_per on r.id_per equals s.id_per
          select new
          {
              id_pro = p.id_pro,
              c_pro= p.c_pro,
              id_reg = r.id_reg,
              id_per= s.id_per,
              c_per = s.c_per,
              c_email = s.c_email
          }).ToList();
Posted
Updated 24-Oct-22 8:41am
Comments
Richard MacCutchan 20-Nov-21 8:01am    
The definition of aquery is for a List<cl_ELAB> (whatever cl_ELAB is), but you are trying to store it as a List<id_pro c_pro, id_reg, id_per, c_per, c_email>. The two List types must match exactly.
BillWoodruff 20-Nov-21 8:03am    
Maybe ?

).Cast<cl_elab>().ToList();
antobaro 21-Nov-21 11:08am    
Thank you very much for your help. :-)

The error is on a type conversion.
try
C#
aquery = (from p in ctx.ty9_prog.Where(t => t.id_pro == 1234)
          join r in ctx.t11_reg on p.id_pro equals r.id_pro
          join s in ctx.t35_per on r.id_per equals s.id_per
          select new cl_ELAB()
          {
              id_pro = p.id_pro,
              c_pro= p.c_pro,
              id_reg = r.id_reg,
              id_per= s.id_per,
              c_per = s.c_per,
              c_email = s.c_email
          }).ToList();
 
Share this answer
 
Comments
antobaro 21-Nov-21 11:09am    
Great !!
I'll use your solution. Thank you very much for your help.
To add to what Estys has - rightly - said ...
When you use select new in a Linq query, it creates an anonymous type unless you explicitly specify the name of an existing type:
var x = (from p in ...
          select new
          {
          X = p.x,
          Y = P.y
          }).ToList();

var y = (from p in ...
          select new MyClass()
          {
          X = p.x,
          Y = P.y
          }).ToList();
The former create a list of an anonymous class, the second creates a list of MyClass objects. Even though the two classes contain identical data, and identical properties, the system cannot and will not consider them the same: MyClass could have methods delegates, events, fields, or even properties that are not a part of the anonymous type and so the two collections are not equivalent, and the anonymous type collection cannot be assigned to a variable of the named class collection.
 
Share this answer
 
Comments
antobaro 21-Nov-21 11:10am    
Thank you very much for your help.
OriginalGriff 21-Nov-21 11:32am    
You're welcome!
After getting this error:
Gravité	Code	Description	Projet	Fichier	Ligne	État de la suppression
Erreur	CS0029	Impossible de convertir implicitement le type 'System.Collections.ObjectModel.ReadOnlyCollection<OpenQA.Selenium.IWebElement>' en 'System.Collections.Generic.List<OpenQA.Selenium.WebElement>'


I escaped to copying:

/// List<WebElement> elementAs = driver.FindElements(By.TagName("body"));
List<WebElement> elementAs = new List<WebElement>();
foreach (WebElement ItemMatch in driver.FindElements(By.TagName("body")))
{
    elementAs.Append(ItemMatch);
}
 
Share this answer
 
v6
Comments
Richard Deeming 25-Oct-22 8:51am    
This question was solved almost a year ago, and your code has nothing to do with the question that was posted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900