LINQ ICollection of a Model to a string with Name and Surname 来自另一个模型
LINQ ICollection of a Model to a string with Name and Surname from within another model
我有一个字段,它是模型 ICollection "Parties" 的集合,它在另一个模型 "Matter" 中。我想提取每一方的姓名和姓氏,并使用 LINQ 用“/”分隔。我将这些放入名为 Investors 的字段中,即倒数第二项。
这是我的查询:
var matterQuery = (from matter in context.Matters
select new
{
matter.ID,
matter.AttorneyRef,
matter.Status,
matter.ProductType,
matter.IsDiscarded,
matter.Base_CreatedDate,
matter.Base_CreatedBy,
matter.BranchID,
matter.InstitutionID,
matter.InvestmentTypeID,
StatusName = matter.MatterStatu != null ? matter.MatterStatu.Name : string.Empty,
ProductTypeName = matter.ProductType1 != null ? matter.ProductType1.Name : string.Empty,
AccountNumber = matter.Account != null ? matter.Account.AccountNumber : string.Empty,
CreatedBy = matter.Paralegal != null ? matter.Paralegal
: matter.AspNetUser != null ? matter.AspNetUser.FirstName + " " + matter.AspNetUser.LastName
: matter.User != null ? matter.User.Firstname + " " + matter.User.Surname
: string.Empty,
IntermediaryName = matter.IntermediaryBranch != null ? matter.IntermediaryBranch.Intermediary.Name
: matter.Attorney != null ? matter.Attorney.AttorneyName
: string.Empty,
CloseRequested = matter.CloseAccounts.Where(x => x.Status == (int)CloseAccountStatusEnum.Authorised).Count() > 0,
StatementRequested = matter.Account != null ? matter.Account.StatementRequested : false,
RequestedDate = matter.RequestedDate.HasValue ? matter.RequestedDate.Value : matter.Base_CreatedDate,
Investors = matter.Parties.Select(x => x.Name.ToString()).Concat(matter.Parties.Select(x => x.Surname.ToString())),
SoftLock = matter.SoftLock,
})
.AsQueryable();
我需要在 1 个长字符串中使用它,因为我正在搜索过滤器的结果和以下行:
(x.Investors != null && x.Investors.ToLower().Contains(search))
要求它是字符串格式。我想将结果显示为 "Name1 Surname1 / Name2 Surname2 / ..."
我该怎么做?
编辑:
我已尝试加入和聚合,但出现错误:
base {System.SystemException} = {System.NotSupportedException:LINQ to Entities 不识别方法 'System.String Join(System.String, System.Collections.Generic.IEnumerable`1[System.String])' 方法,并且此方法无法转换为存储表达式。
在 System.Da...
和...
System.NotSupportedException:LINQ to Entities 无法识别方法 'System.String Aggregate[String](System.Collections.Generic.IEnumerable1[System.String], System.Func
3[System.String,System.String,System.String])'
如果我没有理解你的问题,你有两个选择:
- 从数据库中获取所有结果并在本地进行连接
- 打电话给 sp 来做那件事
勾选这个Is there a "for xml path" equivalent in LINQ to SQL?
假设您的搜索有效并且搜索结果是 IEnumerable<string>
,您可以这样做来创建您想要的字符串:
// IEnumerable<string> searchResult = ... this is your search statement
string result = String.Join(" / ", searchResult);
我通过以下方式解决了这个问题:
在原始查询中:
select new{
Investors = matter.Parties.Select(x => x.Name + " " + x.Surname),
}
在搜索中:
matterQuery.Where(x =>
(x.Investors.Any(y => y.ToLower().Contains(searchString)))
);
将数据传输到 ViewModel 时:
Investors = String.Join(" / ", matter.InvestorNames),
我有一个字段,它是模型 ICollection "Parties" 的集合,它在另一个模型 "Matter" 中。我想提取每一方的姓名和姓氏,并使用 LINQ 用“/”分隔。我将这些放入名为 Investors 的字段中,即倒数第二项。
这是我的查询:
var matterQuery = (from matter in context.Matters
select new
{
matter.ID,
matter.AttorneyRef,
matter.Status,
matter.ProductType,
matter.IsDiscarded,
matter.Base_CreatedDate,
matter.Base_CreatedBy,
matter.BranchID,
matter.InstitutionID,
matter.InvestmentTypeID,
StatusName = matter.MatterStatu != null ? matter.MatterStatu.Name : string.Empty,
ProductTypeName = matter.ProductType1 != null ? matter.ProductType1.Name : string.Empty,
AccountNumber = matter.Account != null ? matter.Account.AccountNumber : string.Empty,
CreatedBy = matter.Paralegal != null ? matter.Paralegal
: matter.AspNetUser != null ? matter.AspNetUser.FirstName + " " + matter.AspNetUser.LastName
: matter.User != null ? matter.User.Firstname + " " + matter.User.Surname
: string.Empty,
IntermediaryName = matter.IntermediaryBranch != null ? matter.IntermediaryBranch.Intermediary.Name
: matter.Attorney != null ? matter.Attorney.AttorneyName
: string.Empty,
CloseRequested = matter.CloseAccounts.Where(x => x.Status == (int)CloseAccountStatusEnum.Authorised).Count() > 0,
StatementRequested = matter.Account != null ? matter.Account.StatementRequested : false,
RequestedDate = matter.RequestedDate.HasValue ? matter.RequestedDate.Value : matter.Base_CreatedDate,
Investors = matter.Parties.Select(x => x.Name.ToString()).Concat(matter.Parties.Select(x => x.Surname.ToString())),
SoftLock = matter.SoftLock,
})
.AsQueryable();
我需要在 1 个长字符串中使用它,因为我正在搜索过滤器的结果和以下行:
(x.Investors != null && x.Investors.ToLower().Contains(search))
要求它是字符串格式。我想将结果显示为 "Name1 Surname1 / Name2 Surname2 / ..."
我该怎么做?
编辑:
我已尝试加入和聚合,但出现错误:
base {System.SystemException} = {System.NotSupportedException:LINQ to Entities 不识别方法 'System.String Join(System.String, System.Collections.Generic.IEnumerable`1[System.String])' 方法,并且此方法无法转换为存储表达式。 在 System.Da...
和...
System.NotSupportedException:LINQ to Entities 无法识别方法 'System.String Aggregate[String](System.Collections.Generic.IEnumerable1[System.String], System.Func
3[System.String,System.String,System.String])'
如果我没有理解你的问题,你有两个选择:
- 从数据库中获取所有结果并在本地进行连接
- 打电话给 sp 来做那件事
勾选这个Is there a "for xml path" equivalent in LINQ to SQL?
假设您的搜索有效并且搜索结果是 IEnumerable<string>
,您可以这样做来创建您想要的字符串:
// IEnumerable<string> searchResult = ... this is your search statement
string result = String.Join(" / ", searchResult);
我通过以下方式解决了这个问题:
在原始查询中:
select new{
Investors = matter.Parties.Select(x => x.Name + " " + x.Surname),
}
在搜索中:
matterQuery.Where(x =>
(x.Investors.Any(y => y.ToLower().Contains(searchString)))
);
将数据传输到 ViewModel 时:
Investors = String.Join(" / ", matter.InvestorNames),