Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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 to entities将列表与逗号分隔的ID进行比较_C#_Entity Framework_Linq - Fatal编程技术网

C# 如何使用linq to entities将列表与逗号分隔的ID进行比较

C# 如何使用linq to entities将列表与逗号分隔的ID进行比较,c#,entity-framework,linq,C#,Entity Framework,Linq,我在用户配置文件中有兴趣ID列感兴趣的ID列表。我想使用实体框架获取与列表中的兴趣ID匹配的用户配置文件 前 Sam 1,5,9,13,4,8 约翰2,7,13,9 凯蒂1,4,8,12,15 列表:{4,8} 我希望输出为{Sam,Kettie} 更新:数据库结构- public class UserProfile { public long UserId { get; set; } public string FullName { get; set; } pu

我在用户配置文件中有兴趣ID列感兴趣的ID列表。我想使用实体框架获取与列表中的兴趣ID匹配的用户配置文件

Sam 1,5,9,13,4,8 约翰2,7,13,9 凯蒂1,4,8,12,15 列表:{4,8}

我希望输出为{Sam,Kettie}

更新:数据库结构-

public class UserProfile
{    
    public long UserId { get; set; }
    public string FullName { get; set; }
    public string Interests { get; set; }  //Store comma separated Interest Ids here 
}

public class Interest
{
    public long InterestId { get; set; }
    public string Name { get; set; }
}
我是通过

var interestIds = db.Interests.Where(i => i.Name.Contains(query))
        .Select(i=> i.InterestId)
        .ToList();
var profiles = new List<UserProfile>();
foreach (var id in interestIds)
{
    profiles.AddRange(db.UserProfiles
        .Where(p=> p.Interests.Contains(id.ToString()))
        .ToList());
}
return profiles;

但是,在处理大量记录时,执行需要很长时间,因此我希望得到帮助以优化这一点。

如果答案不完全符合您的要求,则表示歉意,因为我不熟悉实体框架,但纯粹从C的角度来看它:

对于您感兴趣的每个id,您每次都在筛选整个数据库,并通过检查每个条目上的集合来执行此操作—我可以想象您在这里会受到性能损失

如果可能,您可以将每个兴趣id映射到用户名列表。这只需执行一次,可能会执行得更好,例如:

var dict = new Dictionary<long, List<string>>();
foreach(var user in userProfiles)
{
    foreach(var interest in user.Interests)
    {
        List<string> names;
        if(dict.TryGetValue(interest, out names))
            names.Add(user.Name);
        else
            dict.Add(interest, new[] { user.Name }.ToList());
    }
}

long[] interestIds = new[] { 4, 8 };

HashSet<string> profiles = new HashSet<string>();
foreach (var interestId in interestIds)
    profiles.UnionWith(dict[interestId]);
因此,迭代每个用户的每个兴趣,然后将每个兴趣添加为键,并将用户添加到该键的用户列表中


然后,您可以迭代兴趣列表并从字典中提取匹配列表中的用户名注意哈希集-这将阻止您为匹配多个兴趣的用户获取重复的名称。

这将帮助您仅在sql server中执行一次查询。。我使用探查器检查了它,它更好,因为当foreach循环迭代时。。它会在每次连接时打开和关闭sql Server。。这会导致你的欲望结果慢得多

        var interestIds = db.Interests.Where(i => i.Name.Contains("interest_name"))
                        .Select(i => i.InterestId)
                        .ToList().ConvertAll(i => i.ToString());
        //var profiles = new List<UserProfile>();

        var allProfiles = (from UserProfile up in db.UserProfiles
                           from i in interestIds
                           where up.Interests.Contains(i)
                           select up).ToList();
        return allProfiles;

        //string sId = null;
        //foreach (var id in interestIds)
        //{
        //    sId = id.ToString();
        //    profiles.AddRange(db.UserProfiles
        //        .Where(p => p.Interests.Contains(sId))
        //        .ToList());
        //}
        //return profiles;

亲爱的,请不要误会我,但你的要求听起来像:我有一个任务,请为我做。请阅读堆栈溢出的rools并尝试跟随。你完全可以做到!提供一些你的演示代码…@YaugenVlasau我已经更新了我的问题。我希望这符合规则&你为我提供了一个解决方案:@SarangK因为你在兴趣和用户配置文件中都有Id,所以你应该将它作为它们之间的外键,这样你就可以使用导航属性,因为多个用户配置文件可以共享兴趣,兴趣将属于多个用户配置文件,mn之间应该有一个链接表,其中键由profileid和interestid以及可能的额外信息组成。通过这种方式,可以执行简单的连接来执行查询,这样您就可以轻松地将感兴趣的内容放入查询中,然后通过连接或in-lang导航属性,您还可以获得用户配置文件的数据。感谢您的回答。如果我错了,请纠正我,但对于上面的代码,我需要从数据库中获取所有用户配置文件,而且还有一个嵌套的foreach,所以您不认为它会增加响应时间吗?看起来也有点复杂。在您的示例中,它不是获取所有用户配置文件,然后使用减少它们。每个interestId上的位置?这可能就是我对实体框架缺乏理解的原因!谢谢你的帮助。它起作用了,但只是部分起作用。当我的兴趣Id为1时&一个兴趣为5,11或11,12的用户配置文件,它也会选择这些配置文件,这在意义上是错误的。是的,我有,但我主要与Linq一起使用EF。我不熟悉存储过程。如果你有办法,我就试试。