locked
Why will this lambda expression not work RRS feed

  • Question

  • User-1139055257 posted

    I have a web form using a ListView.  I want to filter the ListView by the loged on user.  I am new to Lambda expressions and have created the following code as my data source.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.ModelBinding;
    
    
    namespace LinkAnalysis
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            // private string dbuser = System.Web.HttpContext.Current.User.Identity.Name; // Used for live site
            private string dbuser = (System.Security.Principal.WindowsIdentity.GetCurrent().Name); //used for developoment
            protected void Page_Load(object sender, EventArgs e)
            {
                using (LAEntities myEntities = new LAEntities()) ;
               
            }
    
            public List<LinkAnalysi> GetLinks()
            {
    
                using (LAEntities db = new LAEntities())
                {
                   //var results = db.LinkAnalysis.ToList();
                   //return results;
                    var results = db.LinkAnalysis.Where(uu => uu.LinkAnalysisCreator == dbuser).ToString();
                    ListView1.DataSource = results;
                    ListView1.DataBind();
                }
            }
    
              
        }
    }

    However when I build the solution I get an error message "Error 5 'LinkAnalysis.WebForm1.GetLinks()': not all code paths return a value"

    Can anyone explai what I have missed here.

     

    Thanks

    Thursday, June 4, 2015 10:03 AM

Answers

  • User1577371250 posted

    Hi,

    public partial class WebForm1 : System.Web.UI.Page
        {
            // private string dbuser = System.Web.HttpContext.Current.User.Identity.Name; // Used for live site
            private string dbuser = (System.Security.Principal.WindowsIdentity.GetCurrent().Name); //used for developoment
            protected void Page_Load(object sender, EventArgs e)
            {
                 if(!IsPostBack)
                 {
                      GetLinks();
                 }
               
            }
    
            public void GetLinks()
            {
                using (LAEntities db = new LAEntities())
                {
                    var results = db.LinkAnalysis.Where(uu => uu.LinkAnalysisCreator == dbuser).ToList();
    
                    ListView1.DataSource = results;
                    ListView1.DataBind();
                }
            }
    
              
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 4, 2015 10:59 AM
  • User753101303 posted

    Hi,

    "Model binding" should be when  using a "SelectMethod" and "ItemType" attributes to tell the control which method should be called to populate himself (rather than having the code "pushing" data to the control).

    Show ListView attributes if it doesn't help.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 4, 2015 1:01 PM
  • User281315223 posted

    "Model binding" should be when  using a "SelectMethod" and "ItemType" attributes to tell the control which method should be called to populate himself (rather than having the code "pushing" data to the control).

    That's kind of what I was thinking as well Patrick.

    Using that approach, you would actually return a List of your objects similar to your original response :

    public List<LinkAnalysi> GetLinks()
    {
        using (LAEntities db = new LAEntities())
        {
              // Get your data to bind it
              return db.LinkAnalysis.Where(uu => uu.LinkAnalysisCreator == dbuser).ToList();
        }
    }

    And then within your ListView, you would define a SelectMethod and ItemType as Patrick mentioned and let the ListView do it's thing :

    <asp:ListView ID="ListView1" runat="server" ItemType="LinkAnalysi" SelectMethod="GetLinks">
        <!-- Row Column Definitions here -->
    </asp:ListView>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 4, 2015 1:07 PM
  • User-1139055257 posted

    I finally got this problem resolved I did not have to make any chnages in the ListView ASP I used the code shown below to filter the results

     public List<LinkAnalysi> GetLinks()
    
            {
                using (LAEntities db = new LAEntities())
                {
                    
                    var results = db.LinkAnalysis.Where(uu => uu.LinkAnalysisCreator == dbuser).ToList();
                    return results;
                        
                    
                }
            }

    Thanks again for all the suggestions

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 4, 2015 2:00 PM

All replies

  • User1577371250 posted

    Hi,

    public partial class WebForm1 : System.Web.UI.Page
        {
            // private string dbuser = System.Web.HttpContext.Current.User.Identity.Name; // Used for live site
            private string dbuser = (System.Security.Principal.WindowsIdentity.GetCurrent().Name); //used for developoment
            protected void Page_Load(object sender, EventArgs e)
            {
                 if(!IsPostBack)
                 {
                      GetLinks();
                 }
               
            }
    
            public void GetLinks()
            {
                using (LAEntities db = new LAEntities())
                {
                    var results = db.LinkAnalysis.Where(uu => uu.LinkAnalysisCreator == dbuser).ToList();
    
                    ListView1.DataSource = results;
                    ListView1.DataBind();
                }
            }
    
              
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 4, 2015 10:59 AM
  • User281315223 posted

    However when I build the solution I get an error message "Error 5 'LinkAnalysis.WebForm1.GetLinks()': not all code paths return a value"

    Can anyone explai what I have missed here.

    This isn't really an issue with your actual lambda expression, which looks fine, but rather the signature of your GetLinks() method. You currently have this method designated to return a List of LinkAnalsi objects as highlighted in the signature below :

    public List<LinkAnalysi> GetLinks() { ... }

    Since this is defined, every possible execution path for your method MUST return an object that could match this specific type (e.g. either an actual List<LinkAnalysi>, null, etc.). Currently your function doesn't return anything at all (as the return statement is commented out). This leaves you with the following two options :

    • Consider changing your return type to "void" since you aren't actually returning an object and are instead just binding your data to a grid :
      public void GetLinks()
      {
          using (LAEntities db = new LAEntities())
          {
                // Get your data and bind it to your Grid
                var results = db.LinkAnalysis.Where(uu => uu.LinkAnalysisCreator == dbuser).ToList();
                ListView1.DataSource = results;
                ListView1.DataBind();
          }
      }
    • Or update your function, retaining your current return type and simply return your List of LinkAnalysi objects :
      public List<LinkAnalysi> GetLinks()
      {
          using (LAEntities db = new LAEntities())
          {
                // Get your data and bind it to your Grid
                var results = db.LinkAnalysis.Where(uu => uu.LinkAnalysisCreator == dbuser).ToList();
                ListView1.DataSource = results;
                ListView1.DataBind();
      
                // Return your results after binding your data
                return results;
          }
      }

    Both of those options should handle what you need.

    Thursday, June 4, 2015 12:22 PM
  • User-1139055257 posted

    Thanks That solved the original problem and I can now build the solution.

    however when I run it I get an error "{"DataSource or DataSourceID cannot be defined on 'ListView1' when it uses model binding."}"  any idea why this is happening and how to correct it?

     

    Thnaks

    Thursday, June 4, 2015 12:29 PM
  • User281315223 posted

    What does your ListView look like? 

    You'll need to be sure that you have some type of ID defined so that the control knows how to map each of your results to it's own within the ListView. I believe there is usually a property called DataKeyNames as seen in the rough example below which will need to correspond to your ID property for your specific objects :

    <asp:ListView ID="ListView1" runat="server" DataKeyNames="LinkAnalysisID" ItemType="LinkAnalysi">
        <!-- Row Column Definitions here -->
    </asp:ListView>

    Thursday, June 4, 2015 12:52 PM
  • User753101303 posted

    Hi,

    "Model binding" should be when  using a "SelectMethod" and "ItemType" attributes to tell the control which method should be called to populate himself (rather than having the code "pushing" data to the control).

    Show ListView attributes if it doesn't help.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 4, 2015 1:01 PM
  • User281315223 posted

    "Model binding" should be when  using a "SelectMethod" and "ItemType" attributes to tell the control which method should be called to populate himself (rather than having the code "pushing" data to the control).

    That's kind of what I was thinking as well Patrick.

    Using that approach, you would actually return a List of your objects similar to your original response :

    public List<LinkAnalysi> GetLinks()
    {
        using (LAEntities db = new LAEntities())
        {
              // Get your data to bind it
              return db.LinkAnalysis.Where(uu => uu.LinkAnalysisCreator == dbuser).ToList();
        }
    }

    And then within your ListView, you would define a SelectMethod and ItemType as Patrick mentioned and let the ListView do it's thing :

    <asp:ListView ID="ListView1" runat="server" ItemType="LinkAnalysi" SelectMethod="GetLinks">
        <!-- Row Column Definitions here -->
    </asp:ListView>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 4, 2015 1:07 PM
  • User-1139055257 posted

    This is what the List View Looks Like

     <asp:ListView ID="ListView1" runat="server" DataKeyNAmes= "LinkalalysisID" ItemType="LinkAnalysis.LinkAnalysi"
                    SelectMethod="GetLinks">

    Thanks for thwo options above.
    option 1 return results resulted in an message in the designer stating " since LinkAnalysis.WebForm1.GetLinks() returns void,return keyword must not be followed by an object expression"

    Option 2 allowed the solution to be built without any erros when it ran I got the messsage shwon above.

     

    I thanks all of you who are providing suggetions to the solve the problem.

     

    Thanks again.

    Thursday, June 4, 2015 1:12 PM
  • User-1139055257 posted

    I finally got this problem resolved I did not have to make any chnages in the ListView ASP I used the code shown below to filter the results

     public List<LinkAnalysi> GetLinks()
    
            {
                using (LAEntities db = new LAEntities())
                {
                    
                    var results = db.LinkAnalysis.Where(uu => uu.LinkAnalysisCreator == dbuser).ToList();
                    return results;
                        
                    
                }
            }

    Thanks again for all the suggestions

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 4, 2015 2:00 PM