none
Filter data in a partila class RRS feed

  • Question

  • Hi,

    I need to run this LINQ query to further filter the list of companies that is shown in a drop down list box in my Dynamic Data Web application.

    from c in tblCompanies 
            where c.tblWorkgroups.Where(wg1 =>   wg1.tblCompany.CompanyName != "")
                            .OrderBy(wg2 => wg2.tblCompany.CompanyName)
                            .Any(wg => wg.tblUsers.Any())
                          select c;

    I have created a partial company class to futher filter the list of companies to be delivered to the DD site but I am sure not what method or event to override.

    Could someone please shed some light?

    namespace MarsUsersAdmin.DAL
    {
        [ScaffoldTable(false)]
        [DisplayColumn("CompanyName", "CompanyName")]
        public partial class tblCompany
        {
           

        }
    }

    Doint that in the partial class would be the best place because all the pages in the DD site that uses this company collection would benefit from it.

    I also need to do the same thing for the Users collection - tblUsers

    namespace MarsUsersAdmin.DAL
    {
        [ScaffoldTable(true)]
        [DisplayName("User")]
        [DisplayColumn("FirstName", "FirstName")]
        public partial class tblUser
        {
          

        }
    }

    This is the LINQ query I need to run:

    from u in users
            where u.tblWorkgroups.Any()
            select u;

    But again I am not sure what method or event to override to achieve the filtering.

    Cheers

    C

    Monday, February 6, 2012 2:41 PM

Answers

  • Hi Allen,

    I have already done. Thanks.

    Currently the best way to achieve this is by using something called Splitting Table in the Entity Framework.

    So I posted my query in the right forum :-)

    Cheers

    C

    Thursday, February 9, 2012 10:11 AM

All replies

  • Hi Claudio;

    Basically in the select clause of the query you pick which fields you want the query to fill and assign them to the public field names of the class. Also not the new keyword in the select clause with the class name to be created.

     

    var results = from c in tblCompanies 
                  where c.tblWorkgroups.Where(wg1 => wg1.tblCompany.CompanyName != "")
                         .OrderBy(wg2 => wg2.tblCompany.CompanyName)
                         .Any(wg => wg.tblUsers.Any())
                  select new tblCompany
                  {
                      ClassFieldName1 = c.FieldInQueryToFillWith,
                      // ...
                      ClassFieldNameN = c.FieldInQueryToFillWith
                  };
    

    namespace MarsUsersAdmin.DAL { [ScaffoldTable(false)] [DisplayColumn("CompanyName", "CompanyName")] public partial class tblCompany { public [DataType] ClassFieldName1 { get; set; } // ... public [DataType] ClassFieldNameN { get; set; } } }

     

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, February 6, 2012 3:11 PM
  • Hi Fernando,

    Thanks for your reply. Let me try to explain better what I have to do:

    In my ASP.NET application I am using an EntityDataSource to explicitly fetch the tblCompany entities. Currently, the EntityDataSource retrieve all companies even the ones with no names and some repeat ones.

    However, I need to use composition to further filter the list of companies.

    I would like to know what method or event of the partial clas I have to use to furter filter the query that the EntityDataSource has created.

    Something like the following?

    namespace MarsUsersAdmin.DAL
    {
        [ScaffoldTable(false)]
        [DisplayColumn("CompanyName", "CompanyName")]
        public partial class tblCompany
        {
               public overrid .....()
               {
                    from c in tblCompanies 
                    where c.tblWorkgroups.Where(wg1 =>   wg1.tblCompany.CompanyName != "")
                            .OrderBy(wg2 => wg2.tblCompany.CompanyName)
                            .Any(wg => wg.tblUsers.Any())
                            select c;

                  }

        }
    }

     

    Cheers

    C

    Monday, February 6, 2012 3:48 PM
  •  

    Sorry Claudio, I have no experience working with ASP.Net and the EntityDataSource so I would not be able to help in this question. When the moderator comes in maybe he can shed some light.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, February 6, 2012 4:17 PM
  • Thanks Fernando.

    Monday, February 6, 2012 4:30 PM
  • Claudio,

    By handling the QueryCreated event of the EntityDataSource you can apply additional filters/ordering to the query. See the link for an example: http://msdn.microsoft.com/en-us/library/ee404748.aspx

    Regards,

    Tyler

    Monday, February 6, 2012 5:09 PM
  • Hi Tyler,

    Thanks for your reply.

    I am using ASP.NET Dynamic Data and the EntityDataSource is used for all the entities you are scaffolding.

    It is not as simple as handling the QueryCreated event of the EntityDataSource.

    Cheers

    C

     

     

    Monday, February 6, 2012 6:31 PM
  • Hi Claudio Pallone,

    Welcome to MSDN Forum.

    Based on the issue, I suggest you to ask in ASP.NET Web Forms Data Controls Forum. I think you can get more help there.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, February 9, 2012 5:19 AM
    Moderator
  • Hi Allen,

    I have already done. Thanks.

    Currently the best way to achieve this is by using something called Splitting Table in the Entity Framework.

    So I posted my query in the right forum :-)

    Cheers

    C

    Thursday, February 9, 2012 10:11 AM
  • Hi,

    As per Fernando sample code above, I have tried using it in my WCF Service to restrict the number of properties returned from the query but I am getting an error:

    "The entity or complex type 'DocketHub_LabellerModel.SiteClient' cannot be constructed in a LINQ to Entities query."

    This is the code I am using. Can someone shed some light please?

     [ServiceContract(Namespace = "")]
        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
        public class LabellerAjaxWCFService
        {
            DocketHubLabellerEntities ctx = new DocketHubLabellerEntities();
            
            [OperationContract]
            public IEnumerable<SiteClient> GetSiteClient(string sortExpression)
            {
                var res = ctx.SiteClients.Where(c => c.ClientName.StartsWith("ci")).OrderBy(sc => sc.ClientName);

                var ret = from i in res
                          select new SiteClient
                          {
                              SiteClientID = i.SiteClientID,
                              ClientName = i.ClientName
                          };
                return ret;

            }

        }

    Cheers

    C

    Wednesday, March 21, 2012 10:08 AM