简体   繁体   中英

Cannot implicitly convert type 'System.Collections.Generic.List<>' to method

I have a class GetSearchFilters_Results which has two lists:

    [DataContract]
    public class GetSearchFilters_Results
    {
        public List<ElementList> ElementList{ get; set; }
        public List<Managers> ManagerList { get; set; }
    }

I have a file called Service.cs:

    public GetSearchFilters_Results GetSearchFilters(string DomainID)
    {
        //Main List return List 
        //List<GetSearchFilters_Results> SearchFilterResults = new List<GetSearchFilters_Results>();

        //Class
        GetSearchFilters_Results GSF = new GetSearchFilters_Results();

        string cs = ConfigurationManager.ConnectionStrings["TestDB"].ConnectionString;
        try
        {
            using (SqlConnection con = new SqlConnection(cs))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("spCPMapp_GetSearchFilters", con);
                cmd.Parameters.AddWithValue("@Domain_Id", DomainID);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
                DataSet Ds = new DataSet();
                sqlDa.Fill(Ds);

                DataTable DtWBS = new DataTable();
                DataTable DTManager = new DataTable();

                sqlDa.Fill(Ds);

                DtWBS = Ds.Tables[0];
                DTManager = Ds.Tables[1];

                //Get WBS Elements List
                if (DtWBS.Rows.Count > 0)
                {
                    List<ElementList> ElementList= new List<ElementList>();
                    for (int i = 0; i < DtWBS.Rows.Count; i++)
                    {
                        ElementList wbs = new ElementList();

                        wbs.ProjectID = Convert.ToInt32(DtWBS.Rows[i]["Project_ID"].ToString());
                        wbs.WBSElementName = DtWBS.Rows[i]["WBSShort"].ToString();

                        WBSElementsList.Add(wbs);
                        //GSF.WBSElementsList.Add(wbs);
                    }
                    GSF.WBSElementsList = WBSElementsList;
                    //SearchFilterResults.Add(GSF);
                }

                //Get Managers List Start
                if (DTManager.Rows.Count > 0)
                {
                    List<Managers> ManagersList = new List<Managers>();
                    for (int i = 0; i < DTManager.Rows.Count; i++)
                    {
                        Managers Mgr = new Managers();

                        Mgr.TimeSheetID = Convert.ToInt32(DTManager.Rows[i]["Project_ID"].ToString());
                        Mgr.ManagerName = DTManager.Rows[i]["Manager"].ToString();

                        //GSF.ManagerList.Add(Mgr);
                        ManagersList.Add(Mgr);
                    }
                    GSF.ManagerList = ManagersList;
                }
                //Manager List End 

            }//Using End 

            //SearchFilterResults.Add(GSF);
        }
        catch (SqlException sqlEx)
        {
            sqlEx.ToString();

        }
        catch (FaultException ex)
        {
            ex.ToString();
        }
        catch (OverflowException e)
        {
            e.ToString();
        }
        return GSF.ManagerList;  // I am getting error, how to add two lists into single objectGetSearchFilters_Results

    }

And another class Elements:

 [DataContract]
 public class Elements
 {

 }

My questions are:

  1. How can I add two separate lists into a single object?

  2. I am getting an error if I add one list into the GetSearchFilter object that says:

    Cannot implicitly convert type to System.Collection.Generic.List to System.Namespace.GetSearchFilterResult() method.

    How do I fix this error?

Your problem is that you are returning a List and not a GetSearchFilter_Results .

If you intend to return a GetSearchFilter_Results object like the function header says, you can change your last line to:

return GSF;

Edit and side note:

You asked how to add two lists together. Assuming the lists are of the same object, you can use the AddRange() function like this:

List<string> stringListA = new List<string>();
List<string> stringListB = new List<string>();
stringListA.AddRange(stringListB); // stringListA now holds the elements from both A and B.

Another addition, hopefully I don't add too much

I've also noticed you don't differentiate how you catch each exception. I don't know what you need for your case, but you can eliminate several lines of code by simply saying:

catch(Exception e)
{
    e.ToString();
}

Instead of multiple catch statements that all do the same thing.

You require the function GetSearchFilters to return

GetSearchFilters_Results 

but what you return really is

GSF.ManagerList

which is of type

List<Managers> ManagerList

That was your error. Anyway for the part 1 pf your question, you can create a class with two data members, each one is a list, then in the constructor or in a separated function, you pass two parameters each for list:

public Class TwoListsClass
{
List <type1> list1;
List <type2> list2;

    public TwoListsClass (List <type1> list1, List <type2> list2)
    {
        this.list1 = list1;
        this.list2 = list2; 
    }

}

then, when you finish the valuating of the two lists you can call the constructor or the function you wrote.

You're posting an awful lot of code for this. Maybe try abstracting away a bit of your logic so it's cleaner and easier to read through?

Your problem is you're attempting to place a List<ManagerList> into a return of GetSearchFilters_Results . Though a List of ManagerList is a property of your GetSearchFilters_Results, they cannot be "implicitly converted" as the error states.

You would want to do something like this potentially:

[DataContract]
public class GetSearchFilters_Results
{
    public List<ElementList> ElementList{ get; set; }
    public List<Managers> ManagerList { get; set; }

    public GetSearchFilters_Results
    {
        ElementList = new List<ElementList>();
        ManagerList = new List<ManagerList>();
    }

    public GetSearchFilters_Results Execute()
    {
        this.ELementList = this.GetElementList();
        this.ManagerList = this.GetManagerList();

        return this;
    }

    public List<ElementList> GetElementList()
    {
        List<ElementList> list = new List<ElementList>();

        // Get list information from db 

        return list;
    }

    public List<ManagerList> GetManagerList()
    {
        List<ManagerList> list = new List<ManagerList>();

        // Get list information from db 

        return list;
    }
}

Wow, this is the older hard way to do things.... consider this: You already have two models.

public List<ElementList> ElementList{ get; set; }
public List<Managers> ManagerList { get; set; }

This tells me is could be generated from EF, which is good....

Now, there's a little known but very cool method in EF that looks like this:

    public List<MyType> QueryThis(ViewModel vm)
    {
        using (var db = new MyEntities()){
        var parms = GetParms(vm);
        var query = Resources.QueryStrings.MyQuery;
        var stuff = db.Database.SqlQuery<MyType>(query, parms);
        return stuff.ToList();
    }

Emphasis on the type being passed into the SQLQuery...

So all you really have to do is provide a new model that combines all the fields you want back and SQL will fill them in for you automatically. The class MyType will have all the fields from both models. SQL also knows how to populate base classes so you can inherit from one class and just enter in fields of the smaller class. No more parsing or even hitting the db more than once. Just tune up the query to get what you need in one shot.

All filtering from this point on can be done using LINQ... it's the new way to use the concepts of ADO.NET disconnected mode. When you dig into this a bit deeper you'll discover repositories, but that's for another day.

var filtered = MyTypeList.Where(p=>p.ManagerName == "SomeValue");
var filtered = MyTypeList.Where(p=>p.ElementType == "Somevalue");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM