LINQ에서 처리 한 코드가 더 좋을 수도 있지만 LINQ 코드의 모양을 모르겠습니다.이 코드를 LINQ로 바꾸기
나는 ItemsItems 컬렉션을 가지고 있으며,이 아이템에는 콜렉션 주석이 있으며, 이러한 필자의 일부는 필 터링하여 한 줄의 문자열로 바뀝니다.
//-- get all comments that is of type "GoodsDescription"
ICollection<FreeText> comments = new List<FreeText>();
foreach (DSV.Services.Shared.CDM.Shared.V2.GoodsItem goodsItem in shipmentInstructionMessage.ShipmentInstruction.ShipmentDetails.GoodsItems)
{
ICollection<DSV.Services.Shared.CDM.Shared.V2.FreeText> freeTexts = goodsItem.Comments.Where(c => c.Type.ToLower() == FREETEXT_TYPE_GOODSDESCRIPTION.ToLower()).ToList();
foreach (DSV.Services.Shared.CDM.Shared.V2.FreeText freeText in freeTexts)
comments.Add(FreeText.CreateFreeTextFromCDMFreeText(freeText));
}
//-- Turn this collection of comments into a single string line
StringBuilder sb = new StringBuilder();
foreach (FreeText comment in comments)
sb.Append(comment.ToString());
contents = sb.ToString();
최초의 Foreach 모든 goodsitems를 통해 루프 및 각 제품의 항목에 대해 내가 주석의 유형이 정의 된 값으로 Equale을 모든 의견을 얻을 : 여기
는 코드입니다.그리고 나서이 새로운 코멘트를 foreach로 가져와 새 Object를 만들고 CommentsCollection에 추가하십시오.
그리고 마지막으로이 commentsColletion을 반복하여 모든 데이터를 단일 문자열 줄로 만듭니다.
LINQ를 사용하여 더 멋지고 스마트 한 방법이 있어야합니다. 당신이 할 수처럼
덕분에 ...
훨씬 좋네요! lambas도 사용할 때 버전이 있습니까 :) –
@Magnus 기술적으로, 우리는 이미이 람다를 가지고 있습니다;). 이를 (컴파일러가하는 것처럼) 메소드 구문에 넣으려면 다음과 같이하십시오. –
첫 _ _ _에서 ... .GoodsItems.SelectMany (goodsItem => goodsItem.Comments.Where (...)) .Select (c => CreateFreeText (c) .ToString()). 공식적인 조언은 쿼리 구문을 사용하는 것이고 나는 그들이 여기 있다고 생각합니다! 어쨌든 도움이 되길 바랍니다. –