Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 249261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:24:45+00:00 2026-05-11T21:24:45+00:00

In this question Jon Skeet offered a very interesting solution to making a LINQ-to-XML

  • 0

In this question Jon Skeet offered a very interesting solution to making a LINQ-to-XML statement dynamic, but my knowledge of lambdas and delegates is not yet advanced enough to implement it:

I’ve got it this far, but of course I get the error “smartForm does not exist in the current context”:

private void LoadWithId(int id)
{
    XDocument xmlDoc = null;
    try
    {
        xmlDoc = XDocument.Load(FullXmlDataStorePathAndFileName);
    }
    catch (Exception ex)
    {
        throw new Exception(String.Format("Cannot load XML file: {0}", ex.Message));
    }

    Func<XElement, bool> whereClause = (int)smartForm.Element("id") == id";

    var smartForms = xmlDoc.Descendants("smartForm")
        .Where(whereClause)
        .Select(smartForm => new SmartForm
                     {
                         Id = (int)smartForm.Element("id"),
                         WhenCreated = (DateTime)smartForm.Element("whenCreated"),
                         ItemOwner = smartForm.Element("itemOwner").Value,
                         PublishStatus = smartForm.Element("publishStatus").Value,
                         CorrectionOfId = (int)smartForm.Element("correctionOfId"),
                         IdCode = smartForm.Element("idCode").Value,
                         Title = smartForm.Element("title").Value,
                         Description = smartForm.Element("description").Value,
                         LabelWidth = (int)smartForm.Element("labelWidth")
                     });

    foreach (SmartForm smartForm in smartForms)
    {
        _collection.Add(smartForm);
    }
}

Ideally I want to be able to just say:

var smartForms = GetSmartForms(smartForm=> (int) smartForm.Element("DisplayOrder").Value > 50);

I’ve got it this far, but I’m just not grokking the lambda magic, how do I do this?

public List<SmartForm> GetSmartForms(XDocument xmlDoc, XElement whereClause)
{
    var smartForms = xmlDoc.Descendants("smartForm")
        .Where(whereClause)
        .Select(smartForm => new SmartForm
                     {
                         Id = (int)smartForm.Element("id"),
                         WhenCreated = (DateTime)smartForm.Element("whenCreated"),
                         ItemOwner = smartForm.Element("itemOwner").Value,
                         PublishStatus = smartForm.Element("publishStatus").Value,
                         CorrectionOfId = (int)smartForm.Element("correctionOfId"),
                         IdCode = smartForm.Element("idCode").Value,
                         Title = smartForm.Element("title").Value,
                         Description = smartForm.Element("description").Value,
                         LabelWidth = (int)smartForm.Element("labelWidth")
                     });
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:24:46+00:00Added an answer on May 11, 2026 at 9:24 pm

    I expect you mean:

    public List<SmartForm> GetSmartForms(
           XDocument xmlDoc, Func<XElement,bool> whereClause)
    

    and:

    Func<XElement, bool> whereClause = smartForm => (int)smartForm.Element("id") == id;
    

    To use as a method, I would make it IEnumerable<T>:

    public static IEnumerable<SmartForm> GetSmartForms(
           XDocument xmlDoc, Func<XElement,bool> predicate)
        {
            return xmlDoc.Descendants("smartForm")
                .Where(predicate)
                .Select(smartForm => new SmartForm
                {
                    ... snip
                });
        }
    

    and call as:

                foreach (SmartForm smartForm in GetSmartForms(xmlDoc,
                    sf => (int)sf.Element("id") == id))
                {
                    _collection.Add(smartForm);
                }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to implement Jon Skeet's solution for this question posted on this blog
Expanding on Jon Skeet's answer to This Previous Question . Skeet doesn't address the
This is just an I am Curious question. In C#-in-depth Jon Skeet says about
Prior to this question, I asked this question on this site. (Mr Jon Skeet
This question was inspired by Jon Skeet's question here where he asked about people
This question is as a result of trying Jon Skeet's answer to this question
When reading Jon Skeet 's answer to this particular question How can I make
In answer to this question here Jon Skeet answered :>> You can test via
This question is inspired by Jon Skeet's answer: Is there a c# equivalent to
In this question @Jon skeet referenced this old blog post by the authority Chris

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.