简体   繁体   English

使用实体框架检索逗号分隔的外键表数据

[英]Retrieve comma separated foreign key table data using Entity Framework

I have a column which saves user id's as comma separated values 我有一列将用户ID保存为逗号分隔的值

'32,12,64' 

What is best way of getting the related table data using Entity Framework if possible as [NotMapped] property in partial class of Task , since there is a trigger to maintain audit history moving user id's to child table is not possible. 如果可能的话,最好使用Entity Framework作为Task类中的[NotMapped]属性来获取相关表数据的最佳方法,因为无法触发维护审计历史记录的操作,将用户ID移至子表。 Please see sample code below 请参见下面的示例代码

public class Task
{
    public int Id { get;set; }
    public string UserIds { get;set; }
    public string UserNames { get;set; }
}

var task = GetCurrentTask()

foreach(var userId in task.UserIds.Split(','))
{
   task.UserNames += GetUserNameFor(userId);
}

Here, UserIDs field is String so, you can query using LINQ if you have the value of this field. 这里,UserIDs字段是String,因此,如果您具有此字段的值,则可以使用LINQ查询。

var userIDs = "32,12,64";
var result = Usertable.retrieve(x=> x.UserID.Contains(userIDs)).ToList();

Try this way. 尝试这种方式。 You can find all the data from the User table with all fields value in results. 您可以从“用户”表中找到所有数据,并在结果中显示“所有字段”值。

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

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