What is the return type for a generic list returning an anonymous 2-string type?

  • A+
Category:Languages

I have this function:

public static List<string> JoinDataTablesMultipleColumns(DataTable dt1, DataTable dt2, string col1, string col2, string col3) {  var multiJoin = from table1 in dt1.AsEnumerable()  join table2 in dt2.AsEnumerable()  on new {    Col1 = table1.Field < string > (col1),    Col2 = table1.Field < string > (col2),    Col3 = table1.Field < string > (col3),  }  equals new {    Col1 = table2.Field < string > (col1),    Col2 = table2.Field < string > (col2),    Col3 = table2.Field < string > (col3),  }  select new {    SeriesID = table1.Field < string > ("SeriesID"),    CatID = table2.Field < string > ("CatID")  };   //if ( multiJoin != null )  //   return multiJoin.ToList();  //else  return null; } 

It works perfectly except that I have the wrong return type. The only way I can compile and run it is by returning null. When I stop the debugger before returning and examine multiJoin.ToList() in the watch window, it's exactly what I want:

What is the return type for a generic list returning an anonymous 2-string type?

But the return type is wrong. I keep trying List<string, string>, but that won't compile. Probably a simple thing but I am stuck. Any ideas?

 


Instead of attempting returning an anonymous type (you can't declare an anonymous return type for your method), create a named type to hold your data:

public class Item {     public string SeriesID { get; set;}     //... } 

and use it instead of the anonymous type that you are currently selecting at the end of your query.

...select new Item {    SeriesID = table1.Field < string > ("SeriesID"),    CatID = table2.Field < string > ("CatID")  }; 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: