如何在 Lambda 表达式中填充 IEnumerable?

发布于 2024-08-09 18:21:03 字数 5468 浏览 8 评论 0原文

我只是无法让它发挥作用,如果有人可以提供帮助,我将不胜感激。

因此,我从数据库中返回一个 XML 结果,如下所示:

<matches>
  <issuer client_name="MTR" score="6" match_list="MTR CORPORATION LIMITED"/>
  <issuer client_name="PEOPLE''S REPUBLIC OF CHINA" score="4"
          match_list="DEMOCRATIC PEOPLE'S REPUBLIC OF KOREA;GOVERNMENT OF THE
          HONG KONG SPECIAL ADMINISTRATIVE REGION OF THE PEOPLE'S REPUBLIC OF
          CHINA;MONGOLIAN PEOPLE'S REPUBLIC;PEOPLE'S DEMOCRATIC REPUBLIC OF
          ALGERIA;PEOPLE'S REPUBLIC OF CHINA"/>
</matches>

在执行一些逻辑(例如匹配 client_name)后,我需要从该 XML 中填充一个对象 我将返回的 XML 结果与我发送到数据库以获取匹配项的结果。

       XDocument.Load(new StringReader(
                              row.GetString(row.GetOrdinal("xml_ret"))))))
                 .Single().Descendants("matches")
                 .Select(x =>
                     new Pool() {
                         Constituents = (IEnumerable<Constituent>)
                            //(... cannot work this how can IsReference populate)
                         //ClientName = x.Attribute("client_name").Value,
                         //Score = x.Attribute("score").Value,
                         //MatchList = x.Attribute("match_list").Value,
                     });

以非 LINQ 方式,我可以像这样填充对象:

foreach (Constituent constituent in pool.Constituents)
        {
            if (!string.IsNullOrEmpty(constituent.Name)
                && string.IsNullOrEmpty(constituent.Curve))
            {
                i++;

                ConstituentMatch match = new ConstituentMatch();
                ConstituentMatch.Group group =new ConstituentMatch.Group("High");

                //High Match group
                ICollection<string> curves = new List<string>();
                curves.Add("EUR" + i);
                curves.Add("USD" + i);

                ICollection<string> names = new List<string>();

                ConstituentMatch.Group.Entry entry =
                    new ConstituentMatch.Group.Entry(constituent.Name + " Ltd.",
                                                     curves);
                group.Add(entry);
                entry =
                    new ConstituentMatch.Group.Entry(constituent.Name + " Inc.",
                                                     curves);
                group.Add(entry);

                match.AddGroup(group);

            }
        }

但是我该怎么办这使用 LINQ,因为我确信你可以做到,但我只是无法解决。

构成类如下所示:

public sealed class Constituent
{
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public ConstituentMatch Match {get;set;}

    public Constituent(string name)
    {
        this.name = name;
    }

    public Constituent() : this(string.Empty) { }
}

构成匹配类如下所示:

public sealed class ConstituentMatch
{
    private readonly Dictionary<string, Group> matches = new Dictionary<string, Group>();

    public IEnumerable<string> GroupNames
    {
        get { return matches.Keys; }
    }

    public Group this[string name]
    {
        get { return matches[name]; }
    }

    public IEnumerable<Group> Groups
    {
        get { return matches.Values; }
    }

    public void AddGroup(Group group)
    {
        matches[group.Name] = group;
    }

    /// <summary>
    /// Match group e.g. Poor, High, All, Begins With
    /// </summary>
    public sealed class Group
    {
        private readonly string name;
        private readonly ICollection<Entry> matches = new List<Entry>();

        public string Name
        {
            get { return name; }
        }

        public Group(string name)
        {
            this.name = name;
        }

        public void Add(Entry entry)
        {
            matches.Add(entry);
        }

        public override bool Equals(object obj)
        {
            bool result = false;
            if (obj is Group)
            {
                Group other = obj as Group;
                result = name == other.name;
            }
            return result;
        }

        public override int GetHashCode()
        {
            return name.GetHashCode();
        }

        public sealed class Entry
        {
            private string legalName;
            private IEnumerable<string> curves;

            private double notional     = double.NaN;
            private char seniriority    = 'N';

            public string LegalName
            {
                get { return legalName; }
            }

            public IEnumerable<string> Curves
            {
                get { return curves; }
            }

            public Entry(string legalName, IEnumerable<string> curves)
                       : this(legalName, curves, double.NaN, 'N') { }

            public Entry(string legalName,
                         IEnumerable<string> curves,
                         double notional,
                         char seniriority)
            {
                this.legalName = legalName;
                this.curves = curves;
                this.notional = notional;
                this.seniriority = seniriority;
            }
        }
    }
}

I just cannot get this to work, would appreciate if someone can help.

So I get back an XML result from a database which looks like:

<matches>
  <issuer client_name="MTR" score="6" match_list="MTR CORPORATION LIMITED"/>
  <issuer client_name="PEOPLE''S REPUBLIC OF CHINA" score="4"
          match_list="DEMOCRATIC PEOPLE'S REPUBLIC OF KOREA;GOVERNMENT OF THE
          HONG KONG SPECIAL ADMINISTRATIVE REGION OF THE PEOPLE'S REPUBLIC OF
          CHINA;MONGOLIAN PEOPLE'S REPUBLIC;PEOPLE'S DEMOCRATIC REPUBLIC OF
          ALGERIA;PEOPLE'S REPUBLIC OF CHINA"/>
</matches>

From this XML I need to populate an object after doing some logic like matching the client_name I am getting back in the XML result to the one I have sent to database to get matches.

       XDocument.Load(new StringReader(
                              row.GetString(row.GetOrdinal("xml_ret"))))))
                 .Single().Descendants("matches")
                 .Select(x =>
                     new Pool() {
                         Constituents = (IEnumerable<Constituent>)
                            //(... cannot work this how can IsReference populate)
                         //ClientName = x.Attribute("client_name").Value,
                         //Score = x.Attribute("score").Value,
                         //MatchList = x.Attribute("match_list").Value,
                     });

In a non-LINQ manner I can populate the object something like this:

foreach (Constituent constituent in pool.Constituents)
        {
            if (!string.IsNullOrEmpty(constituent.Name)
                && string.IsNullOrEmpty(constituent.Curve))
            {
                i++;

                ConstituentMatch match = new ConstituentMatch();
                ConstituentMatch.Group group =new ConstituentMatch.Group("High");

                //High Match group
                ICollection<string> curves = new List<string>();
                curves.Add("EUR" + i);
                curves.Add("USD" + i);

                ICollection<string> names = new List<string>();

                ConstituentMatch.Group.Entry entry =
                    new ConstituentMatch.Group.Entry(constituent.Name + " Ltd.",
                                                     curves);
                group.Add(entry);
                entry =
                    new ConstituentMatch.Group.Entry(constituent.Name + " Inc.",
                                                     curves);
                group.Add(entry);

                match.AddGroup(group);

            }
        }

But how can I do this using LINQ, as I am sure you can do it, I just cannot work it out.

The constituent class looks like:

public sealed class Constituent
{
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public ConstituentMatch Match {get;set;}

    public Constituent(string name)
    {
        this.name = name;
    }

    public Constituent() : this(string.Empty) { }
}

And constituent match class looks like this:

public sealed class ConstituentMatch
{
    private readonly Dictionary<string, Group> matches = new Dictionary<string, Group>();

    public IEnumerable<string> GroupNames
    {
        get { return matches.Keys; }
    }

    public Group this[string name]
    {
        get { return matches[name]; }
    }

    public IEnumerable<Group> Groups
    {
        get { return matches.Values; }
    }

    public void AddGroup(Group group)
    {
        matches[group.Name] = group;
    }

    /// <summary>
    /// Match group e.g. Poor, High, All, Begins With
    /// </summary>
    public sealed class Group
    {
        private readonly string name;
        private readonly ICollection<Entry> matches = new List<Entry>();

        public string Name
        {
            get { return name; }
        }

        public Group(string name)
        {
            this.name = name;
        }

        public void Add(Entry entry)
        {
            matches.Add(entry);
        }

        public override bool Equals(object obj)
        {
            bool result = false;
            if (obj is Group)
            {
                Group other = obj as Group;
                result = name == other.name;
            }
            return result;
        }

        public override int GetHashCode()
        {
            return name.GetHashCode();
        }

        public sealed class Entry
        {
            private string legalName;
            private IEnumerable<string> curves;

            private double notional     = double.NaN;
            private char seniriority    = 'N';

            public string LegalName
            {
                get { return legalName; }
            }

            public IEnumerable<string> Curves
            {
                get { return curves; }
            }

            public Entry(string legalName, IEnumerable<string> curves)
                       : this(legalName, curves, double.NaN, 'N') { }

            public Entry(string legalName,
                         IEnumerable<string> curves,
                         double notional,
                         char seniriority)
            {
                this.legalName = legalName;
                this.curves = curves;
                this.notional = notional;
                this.seniriority = seniriority;
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此刻的回忆 2024-08-16 18:21:03

与此类似的东西应该可以工作

var haystack = new Pool().Constituents;
var indexedhaystack = haystack.Select((item, index)=> new {
    item, index
});
var pool = new Pool()
{
    Constituents = from l in indexedhaystack
                   select new Constituent()
                   {
                        //your stuff here
                   }
};

......扩展

var constituents = new Pool().Constituents.Select((c, i) =>
    new
    {
        Constituent = c,
        Index = i
    });
var items = from c in constituents
            where !string.IsNullOrEmpty(c.Constituent.Name)
                  && string.IsNullOrEmpty(c.Constituent.Curve)
            let curves = new[]{
                            "EUR" + c.Index.ToString(),
                            "USD" + c.Index.ToString()
                               }
            let match = new ConstituentMatch(){
                new Group("High") {
                    new Entry(
                                c.Constituent.Name + " Ltd.",
                                curves),
                    new Entry(
                                c.Constituent.Name + " Inc.",
                                curves)
                }
            }
            select new
            {
                Name = c.Constituent.Name,
                Curves = curves,
                Match = match
            };

......

public class Constituent
{
    public string Name { get; set; }
    public string Curve { get; set; }
}
public class Pool
{
    public List<Constituent> Constituents { get; set; }
}
public class Entry
{
    public Entry(string entry, IEnumerable<string> curves)
    {
    }
}
public class Group : List<Entry>
{
    public Group(string group) { }
}
public class ConstituentMatch : List<Group>
{

}

Some thing similar to this should work

var haystack = new Pool().Constituents;
var indexedhaystack = haystack.Select((item, index)=> new {
    item, index
});
var pool = new Pool()
{
    Constituents = from l in indexedhaystack
                   select new Constituent()
                   {
                        //your stuff here
                   }
};

... extended ...

var constituents = new Pool().Constituents.Select((c, i) =>
    new
    {
        Constituent = c,
        Index = i
    });
var items = from c in constituents
            where !string.IsNullOrEmpty(c.Constituent.Name)
                  && string.IsNullOrEmpty(c.Constituent.Curve)
            let curves = new[]{
                            "EUR" + c.Index.ToString(),
                            "USD" + c.Index.ToString()
                               }
            let match = new ConstituentMatch(){
                new Group("High") {
                    new Entry(
                                c.Constituent.Name + " Ltd.",
                                curves),
                    new Entry(
                                c.Constituent.Name + " Inc.",
                                curves)
                }
            }
            select new
            {
                Name = c.Constituent.Name,
                Curves = curves,
                Match = match
            };

...

public class Constituent
{
    public string Name { get; set; }
    public string Curve { get; set; }
}
public class Pool
{
    public List<Constituent> Constituents { get; set; }
}
public class Entry
{
    public Entry(string entry, IEnumerable<string> curves)
    {
    }
}
public class Group : List<Entry>
{
    public Group(string group) { }
}
public class ConstituentMatch : List<Group>
{

}
白昼 2024-08-16 18:21:03

顾名思义,语言集成查询是一种用于查询对象和数据的技术,而不是修改它们。

Language INtegrated Query is, as its name says, a technology for querying objects and data, not for modifying them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文