deanlee17
asked on
Help with error: Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<string>'
Im getting a slight problem, ive changed the original:
changed to
I thought I solved the problem with
This returns no errors, but when I run the app it returns nothing?!
Thanks,
Dean
lst_CounterfeitParts = (from d in context.QA_CounterfeitParts select (d.PartNumber)).ToList();
changed to
lst_CounterfeitParts = (from d in context.QA_CounterfeitParts select (d.PartNumber, d.Manufacturer)).ToList();
I thought I solved the problem with
lst_CounterfeitParts = (from d in context.QA_CounterfeitParts select d.ToString()).ToList();
This returns no errors, but when I run the app it returns nothing?!
Thanks,
Dean
Your list is typed as a list<String> but the return type of your middle block is returning a list of an anonymous type with 2 params (part number and manufacturer).
Change the declaration to this:
Change the declaration to this:
private var lst_CounterfeitParts;
ASKER
So replacing
with
private List<String> lst_CounterfeitParts = new List<string>();
with
private var lst_CounterfeitParts;
Yes :)
ASKER
The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)
The code site just after:
The code site just after:
public partial class MainWindow : DXRibbonWindow
{
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
While we're talking about it, if you only need to enumerate then you could better define the member as an IEnumerable<MyClass>, and then you don't need to mess around with calling ToList(). I believe all LINQ methods operate on and return IEnumerables.
List<MyClass> lst_CounterfeitParts = new List<MyClass>();
...
lst_CounterfeitParts = (from d in context select new MyClass() { PartNumber = d.PartNumber, Manufacturer = d.Manufacturer });
ASKER
haha ok now one error on:
Could not find an implementation of the query pattern for source type 'Ascent_ERP.AscentEntities '. 'Select' not found.
Could not find an implementation of the query pattern for source type 'Ascent_ERP.AscentEntities
ASKER
Sorry I hadn't seen your post above, will try that now.
ASKER
Still get that error
Are you missing a reference?
using System.Linq;
...actually I think that would fail earlier.
Did you perhaps declare your field as just IEnumerable instead of IEnumerable<MyClass> ?
Did you perhaps declare your field as just IEnumerable instead of IEnumerable<MyClass> ?
ASKER
Sorry I missed this before I left work, I shall reply Monday morning.
If it still doesn't work can you post your code as complete as possible so I can check for any issues?
ASKER
Ok I think ive missed the IEnumerable. So far I have the following:
So should the top part be:
Thanks for the help so far :)
List<MyClass> lst_CounterfeitParts = new List<MyClass>();
lst_CounterfeitParts = (from d in context select new MyClass() { PartNumber = d.PartNumber, Manufacturer = d.Manufacturer });
private class MyClass
{
public String PartNumber;
public String Manufacturer;
}
So should the top part be:
List IEnumerable <MyClass> lst_CounterfeitParts = new List<MyClass>();
Thanks for the help so far :)
The List<T> class implements the IEnumerable<T> interface, i.e. a list is an enumerable but an enumerable is not necessarily a list.
Top part should be:
LINQ methods tend to return generic IEnumerables instead of more specific Lists so if enumerables are sufficient for your needs it's less effort and more flexible to work entirely with them.
Top part should be:
IEnumerable<MyClass> lst_CounterfeitParts = new List<MyClass>();
LINQ methods tend to return generic IEnumerables instead of more specific Lists so if enumerables are sufficient for your needs it's less effort and more flexible to work entirely with them.
ASKER
Could not find an implementation of the query pattern for source type 'Ascent_ERP.AscentEntities '. 'Select' not found.
on this line:
I thought maybe it could not find the table inside the entity framework so I changed it to
This seems to work in that the grid displays the correct number of rows, but the data in each cell is 'Ascent_ERP.MainWindow+MyC lass'
on this line:
lst_CounterfeitParts = (from d in context select new MyClass() { PartNumber = d.PartNumber, Manufacturer = d.Manufacturer });
I thought maybe it could not find the table inside the entity framework so I changed it to
lst_CounterfeitParts = (from d in context.QA_CounterfeitParts select new MyClass() { PartNumber = d.PartNumber, Manufacturer = d.Manufacturer });
This seems to work in that the grid displays the correct number of rows, but the data in each cell is 'Ascent_ERP.MainWindow+MyC
Ok, that looks like progress!
I think the final step is that you're binding the entire MyClass to a field that is expecting a string. The data in the cell is the result of a call to ToString on the class itself (it's trying it's best to get the string).
I imagine in your grid markup you have some line that binds <? =Item ?> or something. Change this to Item.PartNumber or Item.Manufacturer as needed.
I think the final step is that you're binding the entire MyClass to a field that is expecting a string. The data in the cell is the result of a call to ToString on the class itself (it's trying it's best to get the string).
I imagine in your grid markup you have some line that binds <? =Item ?> or something. Change this to Item.PartNumber or Item.Manufacturer as needed.
ASKER
All done.
Thanks very much for all the help.
Just out of interest, the way we have done this using a class, is that common practise and a good template for me to use?
Thanks very much for all the help.
Just out of interest, the way we have done this using a class, is that common practise and a good template for me to use?
Yea I think so.
It's quite explicit which means you can benefit from static analysis (e.g. when it won't compile it you catch bugs early!)
I typically use this pattern for converting (projecting) lists between 2 pre-existing types (List<InputType> --> List<OutputType>).
It's quite explicit which means you can benefit from static analysis (e.g. when it won't compile it you catch bugs early!)
I typically use this pattern for converting (projecting) lists between 2 pre-existing types (List<InputType> --> List<OutputType>).
ASKER
Thanks
ASKER
Open in new window