[英]Pull out Comma Seperated String from EntitySet<T>
How do I convert EntitySet<>
to a String? 如何将
EntitySet<>
转换为字符串? ie (bob, joe, frank)? 即(鲍勃,乔,弗兰克)?
In the below LINQ ContactType
is a seperate table that can have numerous values. 在下面的LINQ
ContactType
是一个单独的表,该表可以具有许多值。 I am trying to convert it into a comma seperated string. 我正在尝试将其转换为逗号分隔的字符串。
var caseNotes =context.tblCaseNotes
.Where(cn => cn.PersonID == personID)
.OrderBy(cn => cn.ContactDate)
.Select(cn => new
{
cn.ContactDate,
cn.ContactDetails,
cn.TimeSpentUnits,
cn.IsCaseLog,
cn.IsPreEnrollment,
cn.PresentAtContact,
ContactType = string.Join(", ", cn.tblCaseNoteContactTypes.ToArray()),
cn.InsertDate,
cn.InsertUser,
cn.CaseNoteID,
cn.ParentNote
});
You have to select which column from tblCaseNoteContactTypes
you want to concat 您必须从
tblCaseNoteContactTypes
选择要连接的列
ContactType = string.Join(", ",
cn.tblCaseNoteContactTypes.Select(x => x.MyColumn))
Append .ToArray();
附加
.ToArray();
at the end of your LINQ query, and use it as parameter for String.Join()
在LINQ查询的末尾,并将其用作
String.Join()
参数
String.Join(", ", yourLinqQuery.ToArray());
For this to work, your LINQ query must return a string, so that the Array is a array of string. 为此,您的LINQ查询必须返回一个字符串,以便Array是一个字符串数组。 Or an object who has a
ToString()
that matches what you want. 或具有匹配您想要的
ToString()
的对象。
String.Join(string, object[]) String.Join(字符串,对象[])
EDIT: as you added new info: 编辑:添加新信息时:
If tblCaseNotesContactTypes is an object, you need to add a .Select(t -> {return t converted to string})
before the .ToArray()
如果tblCaseNotesContactTypes是一个对象,则需要在
.ToArray()
之前添加一个.Select(t -> {return t converted to string})
.ToArray()
Check for empty? 检查是否为空?
ContactType = cn.tblCaseNoteContactTypes.Any() ? string.Join(", ", cn.tblCaseNoteContactTypes.ToArray()) : String.Empty
Also, 也,
tblCaseNoteContactTypes
If the members of this collection aren't strings, Join
is going to have some trouble. 如果此集合的成员不是字符串,则
Join
将遇到一些麻烦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.