开发者

return LINQ to list<entity>?

    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.AI开发者_高级运维CStaffGroupID, 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; }
    }

}


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.


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()*/;


    personList = picp.GetPersonsInChargeList();  // got 6000++ records
    clientList = (List<ClientEntity>)cp.GetClientList();  // got 5000 ++ records after step thru
    //PicList sdf;
    var returnList = (from PersonsInChargeEntity pic in personList
                     join ClientEntity client in clientList
                     on pic.ClientNumber equals client.ClientNumber
                     select new PicList { ClientNumber = pic.ClientNumber, CompanyName = client.CompanyNameFull, AICGroupID = pic.AICStaffGroupID, RPStaffID = pic.RPStaffID, 
                     MPStaffID = pic.MPStaffID, CSPStaffID = pic.CSPStaffID, EQCRStaffID = pic.EQCRStaffID }).ToList();

but returnList doesnt have any records.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜