I have this subquery which is part of a larger query. I want the column called ErrorType in the left hand side to be populated by comma delimited set of values returned by the List on the right hand side. How can i do it ? ErrorTypes is a master whose Key is ErrorTypeID and Value is ErrorTypeName. ProductionErrors is the transaction table whose key is Production ID (supplied by outer query), and whose matching key is ErrorType.
ErrorType = (from errorType in db.ErrorTypes
join prdErrType in db.ProductionErrors
on errorType.ErrorTypeID equals prdErrType.ErrorTypeID
where prdErrType.ProductionID==prd.ProductionID
select errorType.ErrorTypeName).ToList();
在这种情况下,最好使用string.Join
:
ErrorType = string.Join(",", (from...)); // you can omit .ToList() perhaps
you should query something like::
ErrorType = (from errorType in db.ErrorTypes join prdErrType in db.ProductionErrors on errorType.ErrorTypeID equals prdErrType.ErrorTypeID
where prdErrType.ProductionID==prd.ProductionID
select new
{
ErrorType=errorType.ErrorTypeName.tostring()+','
}).ToList()
This is because sometimes LINQ to SQL doesn't support string functions.
string.Join
is the way I would do this, as in the answers that have already been given. But as with most things, there are multiple ways to do it. Another way is using the IEnumerable<T>.Aggregate
method:
var query = (from errorType in db.ErrorTypes
join prdErrType in db.ProductionErrors
on errorType.ErrorTypeID equals prdErrType.ErrorTypeID
where prdErrType.ProductionID==prd.ProductionID
select errorType.ErrorTypeName);
string errorType = query.Aggregate((result,next) => result + "," + next);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.