I have two classes. PostgreSQL and SQLite. The first defines the GetGUIDsFromPG() method in the second GetGUIDsFromSQLite() . Each method returns a List<string> .

Now I need to perform their intersection. Writing:

  DataExistInSQLiteButNotInFB = sqllite.GetGUIDsFromSQLite().Intersect(GetGUIDsFromFB()); 

However, the studio swears that it is impossible to bring the type of one to the type of the other. The question is how to be? As I understand it, the only problem is that they have different classes.

Definition (each in its class):

 public List<string> GetGUIDsFromSQLite() { } 

-

  public List<string> GetGUIDsFromFB() { } 

Can not convert source type 'System.Collections.Generic.IEnumerable to target type' System.Collections.Generic.List '

  • add a complete error message to the question if both functions return a list of strings of problems should not be. Most likely they return something else, or there is an attempt to assign a variable with a different type - Grundy

2 answers 2

As Intersect returns IEnumerable<T> , and you try to assign a variable with the List<T> you get an error.

To solve, call ToList() on the result.

 DataExistInSQLiteButNotInFB = sqllite.GetGUIDsFromSQLite().Intersect(GetGUIDsFromFB()).ToList(); 

or change the type of the DataExistInSQLiteButNotInFB variable to IEnumerable<string>

  • Thanks, and Sharp has no problems with the fact that variables in different classes can not be compared to an example? - Dmitry Bubnenkov
  • @Suliman, did not understand the question. If they have access to this point in the program, then it is possible, if not - no - Grundy
  • for some reason, I have DataExistInSQLiteButNotInFB = sqllite.GetGUIDsFromSQLite().Intersect(GetGUIDsFromFB()).ToList(); such a feeling is not trying to values ​​themselves and objects to cross. Writes: System.Collections.Generic.List 1 [System.String] ` - Dmitry Bubnenkov
  • @Suliman, you need a complete error message - Grundy
  • He simply prints this line. It feels like he is trying to compare two methods, not what they return. - Dmitry Bubnenkov

As an option, make a basic wrapper interface for your database classes and work with objects like this interface.

For example:

 public interface IDbBase { } public class PostgreDB : IDbBase { ... } public class SQLite : IDbBase { ... } 

after that create objects of your classes as

 IDbBase postgre = new PostgreDB(); IDbBase sqlite = new SQLite(); 

and implement your Intersect .