Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.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# 如何使用LINQ从Asp.Net配置文件属性查询_C#_Asp.net_Linq_Profile_Asp.net Profiles - Fatal编程技术网

C# 如何使用LINQ从Asp.Net配置文件属性查询

C# 如何使用LINQ从Asp.Net配置文件属性查询,c#,asp.net,linq,profile,asp.net-profiles,C#,Asp.net,Linq,Profile,Asp.net Profiles,我有Asp.net配置文件属性配置文件。位置,性别等 我需要获得位置属于“伦敦”且性别=男性的所有用户的列表 如何使用LINQ在Asp.net配置文件上执行搜索不,您不能通过默认的Asp.net配置文件提供程序(将配置文件数据保存在数据库中的单个字符串字段中)执行此操作,尽管在另一个数据库表中分离配置文件数据后仍可以执行此操作(这也是一种常见的方法,可以将配置文件数据存储在另一个表中,并通过用户GUID键将其与默认用户表连接),然后您可以使用LINQ查询用户配置文件数据。实际上,您可以这样做。但

我有Asp.net配置文件属性配置文件。位置,性别等

我需要获得位置属于“伦敦”且性别=男性的所有用户的列表


如何使用LINQ在Asp.net配置文件上执行搜索不,您不能通过默认的Asp.net配置文件提供程序(将配置文件数据保存在数据库中的单个字符串字段中)执行此操作,尽管在另一个数据库表中分离配置文件数据后仍可以执行此操作(这也是一种常见的方法,可以将配置文件数据存储在另一个表中,并通过用户GUID键将其与默认用户表连接),然后您可以使用LINQ查询用户配置文件数据。

实际上,您可以这样做。但您需要先做好几件事:

  • 解析并返回序列化属性/值的函数
  • (可选)为每个用户行调用函数的视图。这是可选的,因为某些ORM支持使用用户定义的函数组合查询。我还是建议将视图放置到位
  • 下面是我编写的CLR函数,它解析aspnet_Profile表中的PropertyNames和PropertyValuesString列值。它返回一个包含Property列和Value列的表

    using System.Collections;
    using System.Collections.Generic;
    using System.Data.SqlTypes;
    using System.Linq;
    using System.Text.RegularExpressions;
    using Microsoft.SqlServer.Server;
    
    public partial class UserDefinedFunctions
    {
        private static readonly Regex ProfileRegex = new Regex(@"([a-zA-Z]+):[A-Z]:(\d+):(\d+)");
    
        [SqlFunction(FillRowMethodName = "FillProfileRow",TableDefinition="Property nvarchar(250), Value nvarchar(2000)")]
        public static IEnumerable ParseProfileString(SqlString names, SqlString values)
        {
            var dict = ProfileRegex
                .Matches(names.Value)
                .Cast<Match>()
                .ToDictionary(
                    x => x.Groups[1].Value,
                    x => values.Value.Substring(int.Parse(x.Groups[2].Value), int.Parse(x.Groups[3].Value)));
    
            return dict;
        }
    
        public static void FillProfileRow(object obj, out string Property, out string Value)
        {
            var x = (KeyValuePair<string, string>) obj;
            Property = x.Key;
            Value = x.Value;
        }
    };
    
    瞧,您可以使用SQL或您选择的ORM查询视图。我在Linqpad中写了这个:

    from u in UsersView
    where u.LastName.StartsWith("ove") 
    select u
    

    考虑使用类似的方法:

       matches = 
           matches.Union(
              memberDB.aspnet_Profile
              .Where("it.PropertyValuesString Like @first",
               new ObjectParameter("first", "%<FirstName>%" + firstName + "%</FirstName>%")
               ).Select(p => p.UserId));
    
    匹配项=
    比赛,联盟(
    memberDB.aspnet\u配置文件
    .Where(“类似@first的it.PropertyValuesString”,
    新的ObjectParameter(“第一个”、“%%”+firstName+“%%”)
    ).Select(p=>p.UserId));
    

    我可能会提到我们已经为我们的成员数据库创建了一个EDMX文件。在你有机会的时候,我会考虑把所有这些有趣的信息移到它自己的表中。

    我需要基于配置文件字段搜索和过滤。但是所有的值都存储在ASPNETSL配置文件中的一个字符串中。不建议从aspnet_profile表查询。我可以使用ProfileManager.GetAllProfiles()获取profileInfo集合。并且基本上尝试使用linq进行筛选您是否无法对GetAllProfiles集合执行linq查询?如果是,请查看MSDN,它显示返回类型System.Web.Profile.ProfileInfoCollection为ICollection,IEnumerable,这意味着您无法在其上获取linq。请使用链接t中的代码o get Linq Functional.ProfileInfo对象只包含非常基本的信息,如用户名或LastActivityDate,它不包含在web.config中定义的自定义字段。因此,即使Linq在集合中可用,它也不允许您对自定义字段进行筛选。您需要一个返回ProfileBase对象集合的方法,即d所提供的任何方法都不允许这样做。我给+1,我喜欢你在ParseProfileString()函数中使用的Linq&RegEx的强大功能。谢谢,@bau。我喜欢在序列和函数中思考,所以我的代码大部分时间看起来都像这样。
       matches = 
           matches.Union(
              memberDB.aspnet_Profile
              .Where("it.PropertyValuesString Like @first",
               new ObjectParameter("first", "%<FirstName>%" + firstName + "%</FirstName>%")
               ).Select(p => p.UserId));