简体   繁体   English

如何比较清单 <long> 使用逗号分隔的ID(使用linq实体)

[英]How to compare a List<long> with a comma-separated ids, using linq to entities

I have Interest Ids column in user profile. 我的用户个人资料中有“兴趣Ids列。 & a List<long> interest ids . List<long> interest ids I want to fetch user profiles matching interest ids in List using Entity Framework. 我想使用实体框架在列表中获取与兴趣ID相匹配的用户个人资料。

Ex. 例如

  • Sam 1,5,9,13,4,8 山姆1,5,9,13,4,8
  • John 2,7,13,9 约翰2,7,13,9
  • Kettie 1,4,8,12,15 凯蒂1,4,8,12,15

List: {4,8} 清单:{4,8}

I want output as {Sam,Kettie} 我想输出为{Sam,Kettie}

Update: Db Structure- 更新: Db结构-

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; }
}

I achieve this by 我通过实现

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;

But, when dealing with huge records it takes long time to execute so I want help to optimise this. 但是,在处理大量记录时,它需要很长时间才能执行,因此我需要帮助来优化此记录。

Apologies if the answer is not exactly what you are looking for as I'm unfamiliar with entity-framework, but looking at it purely from a C# point of view: 抱歉,如果答案不完全是您要查找的内容,因为我不熟悉实体框架,而是纯粹从C#的角度来看:

For every id in your interestIds you are filtering your entire database every time (and doing this by checking a collection on each entry) - I would imagine you are incurring a performance penalty here. 对于您的interestIds中的每个id,您每次都在过滤整个数据库(并通过检查每个条目上的集合来做到这一点)-我想您在这里会遭受性能损失。

If possible, you could map each interest id to a list of user names. 如果可能,您可以将每个兴趣ID映射到用户名列表。 This only needs to be done once and may perform better - something like: 这仅需要执行一次,并且可能会表现更好-类似于:

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]);

So iterating over every interest for every user, you then add each interest as a key and add the user to the list of users for that key. 因此,遍历每个用户的每个兴趣,然后将每个兴趣添加为键,并将用户添加到该键的用户列表中。

You can then iterate over your list of interestIds and pull the matching list out user names out of the dictionary (note the HashSet - this will stop you getting duplicate names for users who match more than one interest). 然后,您可以遍历您的interestId列表,并将匹配列表中的用户名从字典中拉出(请注意HashSet-这将使您获得与多个兴趣匹配的用户的重复名称)。

This will help you to execute your query in sql server only once .. I checked it using profiler and it's better because when foreach loop iterate .. it open and close the sql the connection each time.. which leads to populate your desire result much slower.. 这将帮助您sql server only oncesql server only once执行sql server only once查询..我使用事件profiler对其进行了检查,它更好,因为当foreach loop iterate ..每次打开和关闭sql连接时..这导致大量填充您的期望结果慢点..

        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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM