tags:

views:

64

answers:

3
    List<PersonsInChargeEntity> picList = new List<PersonsInChargeEntity>();
    List<ClientEntity> clientList = new List<ClientEntity>();

    var returnList = (from PersonsInChargeEntity pic in picList
                     join ClientEntity client in clientList
                     on pic.ClientNumber equals client.ClientNumber
                     select new { ClientNumber = pic.ClientNumber, CompanyName = client.CompanyNameFull, AICGroupID = pic.AICStaffGroupID, RPStaffID = pic.RPStaffID, MPStaffID = pic.MPStaffID, CSPStaffID = pic.CSPStaffID, EQCRStaffID = pic.EQCRStaffID }).ToList<PicList>();

How can i convert my returnList to a List< entity>? below is my class for the entity.

public class PicList
{
    int _ClientNumber = 0;
    string _CompanyName = "";
    int _AICGroupID = 0;
    int _RPStaffID = 0;
    int _MPStaffID = 0;
    int _CSPStaffID = 0;
    int _EQCRStaffID = 0;

    public PicList()
    {
    }
    public PicList(int ClientNumber, string CompanyName, int AICGroupID, int RPStaffID, int MPStaffID, int CSPStaffID, int EQCRStaffID)
    {
        _ClientNumber = ClientNumber;
        _CompanyName = CompanyName;
        _AICGroupID = AICGroupID;
        _RPStaffID = RPStaffID;
        _MPStaffID = MPStaffID;
        _CSPStaffID = CSPStaffID;
        _EQCRStaffID = EQCRStaffID;
    }

    public int ClientNumber
    {
        get { return _ClientNumber; }
        set { _ClientNumber = value; }
    }
    public string CompanyName
    {
        get { return _CompanyName; }
        set { _CompanyName = value.Trim(); }

    }
    public int AICGroupID
    {
        get { return _AICGroupID; }
        set { _AICGroupID = value; }
    }
    public int RPStaffID
    {
        get { return _RPStaffID; }
        set { _RPStaffID = value; }
    }
    public int MPStaffID
    {
        get { return _MPStaffID; }
        set { _MPStaffID = value; }
    }
    public int CSPStaffID
    {
        get { return _CSPStaffID; }
        set { _CSPStaffID = value; }
    }
    public int EQCRStaffID
    {
        get { return _EQCRStaffID; }
        set { _EQCRStaffID = value; }
    }

}
+4  A: 

From my understanding of your problem, you require the result of the query to be a List<PicList>. To accomplish this, you need to select an instance of the concrete PicList type within your LINQ query rather than of an anonymous type. C# doesn't normally support duck-typing (with some exceptions); in particular, it's not possible to cast one type to another just because the two types happen to have similar property declarations.

I also observe that PicList has a public parameterless constructor, and that all the properties have public setters. So this should work fine:

var returnList = (from PersonsInChargeEntity pic in picList
                  join ClientEntity client in clientList
                  on pic.ClientNumber equals client.ClientNumber
                  select new PicList // no longer an anonymous type
                  { 
                     ClientNumber = pic.ClientNumber, 
                     CompanyName = client.CompanyNameFull,
                     AICGroupID = pic.AICStaffGroupID, 
                     RPStaffID = pic.RPStaffID, 
                     MPStaffID = pic.MPStaffID, 
                     CSPStaffID = pic.CSPStaffID,
                     EQCRStaffID = pic.EQCRStaffID
                  }).ToList(); 
  //ToList<PicList>() is fine but redundant: the generic type argument is inferred.
Ani
thank you, had some trouble understanding linq but it is clearer now.
Derek Long
+2  A: 

you are creating an anonymous type here, which can't be casted like you've tried.

try something like:

var returnList = (from PersonsInChargeEntity pic in picList
                  join ClientEntity client in clientList
                  on pic.ClientNumber equals client.ClientNumber
                  select new PicList
                  {
                    ClientNumber = pic.ClientNumber,
                    CompanyName = pic.CompanyNameFull,
                    AICGroupID = pic.AICStaffGroupID,
                    RPStaffID = pic.RPStaffID,
                    MPStaffID = pic.MPStaffID,
                    CSPStaffID = pic.CSPStaffID,
                    EQCRStaffID = pic.EQCRStaffID
                  })/*.ToList()*/;
Andreas Niedermair
thanks mate, it works
Derek Long
A: 

Hmmm, You can do it by very basic method linke this

 List<ClientEntity> clientList = new List<ClientEntity>();

var returnList = (from PersonsInChargeEntity pic in picList
                 join ClientEntity client in clientList
                 on pic.ClientNumber equals client.ClientNumber
                 select pic ;

foreach(var item in pic) { ClientEntity obj = new ClientEntity();

obj.ClientNumber  =  item.ClientNumber 

...... ...... clientList.add(obj);

}

return clientlist;

I hope this will run for you as well

Rohit Asthana