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

|| 我有Asp.net配置文件属性Profile.Location,性别等 我需要获取其位置属于“伦敦”且性别=男性的所有用户的列表 我如何使用LINQ在Asp.net配置文件上执行搜索     
已邀请:
实际上,您可以做到。但是,您需要首先将以下几点放在适当的位置: 解析并返回序列化属性/值的函数 (可选)为每个用户行调用该函数的视图。这是可选的,因为某些ORM支持使用用户定义的函数来组成查询。我还是建议将视图放置在适当的位置。 这是我编写的CLR函数,用于解析aspnet_Profile表中的PropertyNames和PropertyValuesString列值。它返回一个带有“属性”列和“值”列的表。
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;
    }
};
部署该功能,然后为用户的配置文件数据创建一个视图。这是一个例子:
CREATE VIEW UsersView
AS

SELECT *
FROM (
    SELECT u.UserId
        ,u.Username
        ,m.Email
        ,f.Property
        ,f.Value
    FROM aspnet_Profile p
    INNER JOIN aspnet_Users u ON p.UserId = u.UserId
    INNER JOIN aspnet_Membership m ON m.UserId = u.Userid
    INNER JOIN aspnet_Applications a ON a.ApplicationId = m.ApplicationId
    CROSS APPLY ParseProfileString(p.PropertyNames, p.PropertyValuesString) f
    WHERE a.ApplicationName = \'MyApplication\'
    ) src
pivot(min(value) FOR property IN (
            -- list your profile property names here
            FirstName, LastName, BirthDate
            )) pvt
瞧,您可以使用SQL或您选择的ORM查询视图。我在Linqpad中写了这个:
from u in UsersView
where u.LastName.StartsWith(\"ove\") 
select u
    
不,您不能通过默认的ASP.NET概要文件提供程序(将概要文件数据保存在数据库的单个字符串字段中)执行此操作,尽管在将另一个数据库表中的概要文件数据分开后仍然可以执行此操作(这很常见)也可以将您的个人资料数据存储在另一个表中,并通过User GUID键将其与默认用户表关联起来),然后您可以使用LINQ查询您的用户个人资料数据。     
考虑使用这样的东西:
   matches = 
       matches.Union(
          memberDB.aspnet_Profile
          .Where(\"it.PropertyValuesString Like @first\",
           new ObjectParameter(\"first\", \"%<FirstName>%\" + firstName + \"%</FirstName>%\")
           ).Select(p => p.UserId));
我可能应该提到,我们已经为会员数据库创建了一个edmx文件。就是说,如果有机会,我会考虑将所有这些有趣的信息移到应用程序数据库中自己的表中。     

要回复问题请先登录注册