2016-12-29 1 views
0

업데이트 시간에 따라 서버 기반 파일 (PDF)을 다운로드하려고합니다 (데이터베이스에 저장 됨). 다운로드하기 전에 로컬 컴퓨터의 기존 파일 목록과 비교해보고자합니다. 문제는 개체 필드를 비교하는 것은 잘못된 출력을 제공합니다. 두 개체 비교 C# UWP

두 파일 모두 주어진 "ITEMS"개체에서 파싱 된 json 기반 파일입니다. C#으로 Visual Studio 2015를 사용하고 있습니다. 백엔드는 Laravel REST입니다.

class Items 
{ 
    public int id { get; set; } 
    public string branch { get; set; } 
    public string item { get; set; } 
    public string link { get; set; } 
    public int active { get; set; } 
    public string created_at { get; set; } 
    public string updated_at { get; set; } 
} 

이 내가 서버와 로컬 목록 (모두 JSON) 구문 분석하고 어떻게 다음 updated_at 필드가

// Compare files 

foreach (Items item in files) 
{ 
    foreach(Items it in filesToDownload) 
    { 
     if (!it.updated_at.Equals(item.updated_at)) 
     { 
     //Download the file 
     //Create a new list of files to download 
     } 
    } 
} 
같거나하지 않으면 내가 확인하는 foreach 루프를 사용하고

for (int i = 0; i < obj.Count; i++) 
{ 
    JObject row = JObject.Parse(obj[i].ToString()); 
    Items newItem = new Items(); 
    newItem.id = int.Parse(row["id"].ToString()); 
    newItem.branch = row["branch"].ToString(); 
    newItem.item = row["item"].ToString(); 
    newItem.link = row["link"].ToString(); 
    newItem.created_at = row["created_at"].ToString(); 
    newItem.updated_at = row["updated_at"].ToString(); 
    files.Add(newItem); 
} 

답변

1
for (int i = 0; i < obj.Count; i++) 
{ 
    JObject row = JObject.Parse(obj[i].ToString()); 
    Items newItem = new Items(); 
    newItem.id = int.Parse(row["id"].ToString()); 
    newItem.item = row["branch"].ToString(); 
    newItem.link = row["item"].ToString(); 
    newItem.item = row["link"].ToString(); 
    newItem.item = row["created_at"].ToString(); 
    newItem.item = row["updated_at"].ToString(); 
    files.Add(newItem); 
} 

모든 변수에 item 속성을 설정하고 있습니다.

시도 : 목록 정말 compareable 그리고 그들은 정확히 동일한 형식을 가지고위한 C#을 item.updated_at 말한다면

for (int i = 0; i < obj.Count; i++) 
{ 
    JObject row = JObject.Parse(obj[i].ToString()); 
    Items newItem = new Items(); 
    newItem.id = int.Parse(row["id"].ToString()); 
    newItem.branch = row["branch"].ToString(); 
    newItem.item = row["item"].ToString(); 
    newItem.link = row["link"].ToString(); 
    newItem.created_at = row["created_at"].ToString(); 
    newItem.updated_at = row["updated_at"].ToString(); 
    files.Add(newItem); 
} 
+0

예. 수정 됨. –

0

복사 + 붙여 넣기가 너무 많이 ... =)

for (int i = 0; i < obj.Count; i++) 
{ 
    JObject row = JObject.Parse(obj[i].ToString()); 
    Items newItem = new Items(); 
    newItem.id = int.Parse(row["id"].ToString()); 
    newItem.item = row["branch"].ToString(); 
    newItem.link = row["item"].ToString(); 
    newItem.item = row["link"].ToString(); 
    newItem.item = row["created_at"].ToString(); 
    newItem.item = row["updated_at"].ToString(); 
    files.Add(newItem); 
} 

newItem.item을 여러 번 반복했습니다.

+1

감사합니다. 수정 됨. stackoverflow에 복사하는 동안 실수였습니다. –

1

는 함수가 교차 또는 사용한 것을 제외하고 "기본적으로"목록을 비교 할 수있는 가능성을 제공합니다;

프로그램이 타당성을 검사하지 않는다는 것을 명심하십시오. 그래서 배열을 반복하면서 여전히 잘못된 결과를 얻었을 것이라고 생각되면 문제가 될 수 있는지 데이터로 확인합니다. .

마치 foreach 루프가 Intersect와 Except처럼 똑같이 할 것 같기 때문에.

시작 루프를 디버깅하고 Expression Checker (또는 OzCode와 같은 쉬운 코드)를 사용하여 직접 목록을 비교하십시오.

나는 또한 타임 스탬프를 사용하는 대신 ID를 비교하는 팬입니다. 호환성이있는 경우.

두 유용한 링크 :

Intersect Two Lists in C#

The opposite of Intersect()

+0

이 람다 식은 너무 복잡합니다. 그래서 foreach 루프를하려고합니다. 당신의 도움을 주셔서 감사합니다. –

+0

코드 조각 작성을 도와 줄 수 있습니까? –