Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果可用,请使用appguid,否则请在LINQ中使用硬编码guid_C#_Linq_Lambda_Active Directory - Fatal编程技术网

C# 如果可用,请使用appguid,否则请在LINQ中使用硬编码guid

C# 如果可用,请使用appguid,否则请在LINQ中使用硬编码guid,c#,linq,lambda,active-directory,C#,Linq,Lambda,Active Directory,我如何在lambda表达式下面写入内容,以便它可以检查匹配的appguid,如果找不到匹配的appguid,那么它将查找硬编码的guid public static string ActiveDirectory(string xmlPath, string applicationGUID,string Element) { XDocument dbConfig = XDocument.Load(xmlPath); return (dbConfig

我如何在lambda表达式下面写入内容,以便它可以检查匹配的appguid,如果找不到匹配的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>

69b150127e7d43efa0e3e896b94953de
真的
LDAP://test.org
测试1
3773e594efga42688cd5113cf316d4d3
真的
LDAP://test.org
测试2

使用第二个查询返回默认值,例如

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不是静态Guid的情况下提供排序优先级,然后使用
FirstOrDefault
获得最高优先级匹配,在这种情况下,
ag.Value==applicationGuid
。请注意,我不能首先确保
a.Element(…)
不是空的,因为上面的
Where
会过滤掉任何空的地方。

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


要想解决这个问题,最简单的方法是只运行两个查询。首先检查传递的ID是否匹配,如果不匹配,请查找与您的硬编码值匹配的项。

您发布的内容有什么问题?如果我的xml文件中有匹配的appguid和硬编码的guid,则会出现错误。我想先检查是否有匹配的appguid,如果没有匹配的appguid,则只查找硬编码的。请举例说明产生错误的数据和您收到的实际错误。