0

如何在 lambda 表达式下面编写,以便它可以检查匹配的 appguid,如果找不到,那么它将寻找硬编码的 guid?

public static string ActiveDirectory(string xmlPath, string applicationGUID,string Element)
{
    XDocument dbConfig = XDocument.Load(xmlPath);

    return (dbConfig
                     .Descendants("Zone")
                     .Where(a =>
                     {
                         XElement ag = a.Element("ApplicationGUID");
                         return ag != null &&
                                (ag.Value == applicationGUID || ag.Value == "3773e594efga42688cd5113cf316d4d3");
                     })
                     .Select(
                         a =>
                         {
                             XElement cs = a.Element(Element);
                             return cs == null
                                        ? null
                                        : cs.Value;
                         })
                     .SingleOrDefault());
}

这就是我的 xml 的样子

<Zone>

        <ApplicationGUID>69b150127e7d43efa0e3e896b94953de</ApplicationGUID>
        <isActiveDirectory>true</isActiveDirectory>
        <ActiveDirectoryPath>LDAP://test.org</ActiveDirectoryPath>
        <DomainName>test1</DomainName>
    </Zone>
  <Zone>
           <ApplicationGUID>3773e594efga42688cd5113cf316d4d3</ApplicationGUID>
    <!--Default App guid-->
    <isActiveDirectory>true</isActiveDirectory>
    <ActiveDirectoryPath>LDAP://test.org</ActiveDirectoryPath>
    <DomainName>test2</DomainName>
  </Zone>
</Zones>
4

2 回答 2

0

SingleOrDefault()在语句的末尾进行选择。Where()在您的示例数据中,您与提供的Func有两个匹配项。SingleOrDefault如果有单个匹配,则返回单个成员,如果没有匹配则返回默认类型,或者抛出异常。在您的情况下,有两个匹配项,因此是一个例外。

看起来你正在尝试做的最简单的解决方案是只运行两个查询。首先检查传递的 ID 是否匹配,如果没有,则查找与您的硬编码值匹配的内容。

于 2013-06-11T21:37:31.887 回答
0

默认返回第二个查询,例如...

public static string ActiveDirectory(string xmlPath, string applicationGUID, string Element)
{
    XDocument dbConfig = XDocument.Load(xmlPath);

    var ret = (dbConfig
                     .Descendants("Zone")
                     .Where(a =>
                     {
                         XElement ag =
                             a.Element("ApplicationGUID");

                         return ag != null &&
                                (ag.Value == applicationGUID);
                     })
                     .SingleOrDefault());

    if(ret == null)
       ret = (dbConfig
                     .Descendants("Zone")
                     .Where(a =>
                     {
                         XElement ag =
                             a.Element("ApplicationGUID");

                         return ag != null &&
                                (ag.Value == "3773e594efga42688cd5113cf316d4d3");
                     })
                     .SingleOrDefault());

    if(ret != null)
    {
           XElement cs = ret.Element(Element);

           return cs == null ? null : cs.Value;
    }

    return null;
}

你可以用诡计使它成为一个单一的陈述,但为什么要混淆这个陈述。假设您的 XML 不是很长,并且该函数没有被反复调用,这可能不会成为您的瓶颈,同时使代码更易于阅读和理解。

但是,如果您真的只想要一个语句,那么您可以尝试使用OrderBy设置优先级,例如:

public static string ActiveDirectory(string xmlPath, string applicationGUID,string Element)
{
XDocument dbConfig = XDocument.Load(xmlPath);

return (dbConfig
                 .Descendants("Zone")
                 .Where(a =>
                 {
                     XElement ag =
                         a.Element("ApplicationGUID");

                     return ag != null &&
                            (ag.Value == applicationGUID || ag.Value == "3773e594efga42688cd5113cf316d4d3");
                 })
                 .OrderBy(a => a.Element("ApplicationGUID").Value == "3773e594efga42688cd5113cf316d4d3" ? 1 : 0)
                 .Select(
                     a =>
                     {
                         XElement cs =
                             a.Element(Element);

                         return cs == null
                                    ? null
                                    : cs.Value;
                     })
                 .FirstOrDefault());
}

OrderBy会在 Guid 不是静态FirstOrDefault的情况下给出排序优先级,然后用于获得最高优先级匹配,这将是ag.Value == applicationGuid. 请注意,我不a.Element(...)首先确定 is not null 因为我Where上面的它将过滤掉它为 null 的任何地方。

于 2013-06-11T21:35:11.703 回答