LINQ Tips #1--Get a list of interface

   In the most of the case, we can use the LINQ to filter out a list of the object collection. But if we are designing a framework which means we will use the interface talk to each other. The collection has to be the container of a set of interface which will need some tricks to finish this job.

The interface is like this(As a quick example, I will create an extreme simple interface :))

   1:  public interface IName
   2:      {
   3:          string FirstName{get;set;}
   4:          string LastName { get; set; }
   5:          string Prefix { get; set; }
   6:      }
 
The implementation of this interface is 
   1:  public class Name : IName
   2:      {
   3:          #region IName Members
   4:   
   5:          public string FirstName
   6:          {
   7:              get;
   8:              set;
   9:          }
  10:   
  11:          public string LastName
  12:          {
  13:              get;
  14:              set;
  15:          }
  16:   
  17:          public string Prefix
  18:          {
  19:              get;
  20:              set;
  21:          }
  22:   
  23:          #endregion
  24:      }
 
First, We will create a helper function to get a list of the names.
 
   1:  private List<IName> GetNames()
   2:          {
   3:              List<IName> list = new List<IName>();
   4:              list.Add(new Name
   5:              {
   6:                  FirstName = "Tom",
   7:                  LastName = "Cruise",
   8:                  Prefix = "Mr."
   9:              });
  10:   
  11:              list.Add(new Name
  12:              {
  13:                  FirstName = "Rose",
  14:                  LastName = "Tommy",
  15:                  Prefix = "Ms"
  16:              });
  17:              return list;
  18:   
  19:          }
 
At the very beginning, the LINQ code will be written like this 
 
List<IName> MenList = (from men in list
where men.Prefix == "Mr"
select
(new Name { FirstName = men.FirstName, LastName = men.LastName, Prefix = men.Prefix })
).ToList();
But you will get the compiler error like this
 
Error    1    Cannot implicitly convert type 'System.Collections.Generic.List<LinqToInterface.Name>' to 'System.Collections.Generic.List<LinqToInterface.IName>'    C:\Learning\Linq\LinqToInterface\LinqToInterface\Form1.cs    27    18    LinqToInterface
In this case, the compiler does not know that the Name is an implementation of the interface IName, so we need to use some other ways around.The way I can help to change the code to this 
   1:   List<IName> list = GetNames();
   2:   
   3:              List<IName> MenList = (from men in list
   4:                                     where men.Prefix == "Mr"
   5:                                     select (IName)
   6:                                     (new Name { FirstName = men.FirstName, LastName = men.LastName, Prefix = men.Prefix })
   7:                  ).ToList();

 

I hope this will help you when you need it.

del.icio.us Tags:
Published Monday, November 10, 2008 3:25 PM by RobertNet
Filed under: ,

Comments

# re: LINQ Tips #1--Get a list of interface

Monday, November 10, 2008 6:15 PM by Eric

Couldn't you just do this:

List<IName> MenList = (from men in list

                      where men.Prefix == "Mr"

                      select men).ToList();

# re: LINQ Tips #1--Get a list of interface

Monday, November 10, 2008 10:10 PM by RobertNet

In this specific case, the answer is YES.

But this is to talk about in general to return a list of interface.

# re: LINQ Tips #1--Get a list of interface

Monday, May 11, 2009 5:01 AM by chris.garcia

I am trying to do the same thing in a linq to SQL context.

I was forced to send back IList<EntityRef> for the exact same cast reasons.

Do you mean that just initializing the result list with dummy Name objects is enough to tell the compiler how to cast the query results into IList<IName> ?

Thanks for your help.

# re: LINQ Tips #1--Get a list of interface

Thursday, September 10, 2009 11:24 AM by InnerSphere

This is exactly what I needed - thanks for the great article!

# re: LINQ Tips #1--Get a list of interface

Thursday, April 01, 2010 6:41 PM by Soe Tun

This is really useful :-) Thanks!

# re: LINQ Tips #1--Get a list of interface

Tuesday, November 16, 2010 5:06 PM by Paul Smith

This unfortunately didn;t work for me. I did find a solution that did work for this problem when it happened for  me.

List<IName> list = GetNames();

             List<IName> MenList = (from men in list

                                    where men.Prefix == "Mr"

                                    select

                                    (new Name { FirstName = men.FirstName, LastName = men.LastName, Prefix = men.Prefix })

                 ).OfType<IName>().ToList();

// No preview I hope this is formatted ok :(

# re: LINQ Tips #1--Get a list of interface

Tuesday, March 27, 2012 6:56 PM by Jordan

Yeah, it happens sometimes ... Nothing special.

# re: LINQ Tips #1--Get a list of interface

Monday, January 28, 2013 4:59 AM by qWpvQTJwHKnIZlzPO

Hyxe5a This is one awesome article.Really looking forward to read more.

Leave a Comment

(required) 
(required) 
(optional)
(required)