Solved

Help with error: Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<string>'

Posted on 2013-12-13
21
4,206 Views
Last Modified: 2013-12-16
Im getting a slight problem, ive changed the original:

lst_CounterfeitParts = (from d in context.QA_CounterfeitParts select (d.PartNumber)).ToList();

Open in new window


changed to

lst_CounterfeitParts = (from d in context.QA_CounterfeitParts select (d.PartNumber, d.Manufacturer)).ToList();

Open in new window


I thought I solved the problem with

lst_CounterfeitParts = (from d in context.QA_CounterfeitParts select d.ToString()).ToList();

Open in new window


This returns no errors, but when I run the app it returns nothing?!

Thanks,
Dean
0
Comment
Question by:deanlee17
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 10
21 Comments
 

Author Comment

by:deanlee17
ID: 39716914
Sorry I should have included this:

private List<String> lst_CounterfeitParts = new List<string>();

Open in new window

0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39717056
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:
private var lst_CounterfeitParts;

Open in new window

0
 

Author Comment

by:deanlee17
ID: 39717094
So replacing

private List<String> lst_CounterfeitParts = new List<string>(); 

Open in new window


with

private var lst_CounterfeitParts; 

Open in new window

0
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39717109
Yes :)
0
 

Author Comment

by:deanlee17
ID: 39717125
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:

public partial class MainWindow : DXRibbonWindow
    {

Open in new window

0
 
LVL 11

Accepted Solution

by:
Angelp1ay earned 500 total points
ID: 39717193
Urgh sorry that's because it's defined as a field in the class not inside a method.

Ok... well instead let's just get explicit!

Create a private class:
        private class MyClass {
            public String PartNumber;
            public String Manufacturer;
        }

Open in new window


Declare your field member to be a list of that type:
List<MyClass> lst_CounterfeitParts = new List<MyClass>();

Open in new window


...and update your LINQ to this:
    this.lst_CounterfeitParts = (from d in context select new MyClass() { PartNumber = d.PartNumber, Manufacturer = d.Manufacturer }).ToList();

Open in new window

0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39717202
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 });

Open in new window

0
 

Author Comment

by:deanlee17
ID: 39717218
haha ok now one error on:

Could not find an implementation of the query pattern for source type 'Ascent_ERP.AscentEntities'.  'Select' not found.
0
 

Author Comment

by:deanlee17
ID: 39717221
Sorry I hadn't seen your post above, will try that now.
0
 

Author Comment

by:deanlee17
ID: 39717234
Still get that error
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39717413
Are you missing a reference?
using System.Linq;

Open in new window

0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39717471
...actually I think that would fail earlier.

Did you perhaps declare your field as just IEnumerable instead of IEnumerable<MyClass> ?
0
 

Author Comment

by:deanlee17
ID: 39718749
Sorry I missed this before I left work, I shall reply Monday morning.
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39718751
If it still doesn't work can you post your code as complete as possible so I can check for any issues?
0
 

Author Comment

by:deanlee17
ID: 39720917
Ok I think ive missed the IEnumerable. So far I have the following:

List<MyClass> lst_CounterfeitParts = new List<MyClass>();

Open in new window


lst_CounterfeitParts = (from d in context select new MyClass() { PartNumber = d.PartNumber, Manufacturer = d.Manufacturer });

Open in new window


        private class MyClass
        {
            public String PartNumber;
            public String Manufacturer;
        } 

Open in new window



So should the top part be:

List IEnumerable <MyClass> lst_CounterfeitParts = new List<MyClass>();

Open in new window


Thanks for the help so far :)
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39721015
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:
IEnumerable<MyClass> lst_CounterfeitParts = new List<MyClass>();

Open in new window


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.
0
 

Author Comment

by:deanlee17
ID: 39721079
Could not find an implementation of the query pattern for source type 'Ascent_ERP.AscentEntities'.  'Select' not found.

on this line:

lst_CounterfeitParts = (from d in context select new MyClass() { PartNumber = d.PartNumber, Manufacturer = d.Manufacturer });

Open in new window


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

Open in new window


This seems to work in that the grid displays the correct number of rows, but the data in each cell is 'Ascent_ERP.MainWindow+MyClass'
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39721125
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.
0
 

Author Comment

by:deanlee17
ID: 39721153
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?
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39721305
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>).
0
 

Author Comment

by:deanlee17
ID: 39721317
Thanks
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question