2008-09-18 3 views

답변

4

당신은 당신은 IEnumerable와 크리스탈 리포트 데이터를 제공 할 수 있습니다, 당신은 엄격 보고서 SetDataSourceDataSet를 사용할 필요가 없습니다, 당신의 LINQ의 결과가 List로 설정 변환 할 수 있습니다. ListIEnumerable에서 상속되므로 보고서의 데이터 소스를 목록으로 설정할 수 있으므로 LINQ 결과 집합에서 .ToList() 메서드를 호출하기 만하면됩니다. 기본적으로

 CrystalReport1 cr1 = new CrystalReport1(); 

     var results = (from obj in context.tSamples 
         where obj.ID == 112 
         select new { obj.Name, obj.Model, obj.Producer }).ToList(); 

     cr1.SetDataSource(results); 
     crystalReportsViewer1.ReportSource = cr1; 
1

나는 자신을 시도하지 않았지만 DataContext.LoadOptions를 사용하여 관계를 받아들이도록하고 GetCommand (IQueryable)를 사용하여 관계를 유지하는 SQLCommand 개체를 반환하는 것이 가능할 것으로 보인다.

MSDN Forums에 대한 자세한 정보를 참조하십시오.

2

msdn doc은 Crystal Report를 ICollection에 바인딩 할 것을 제안합니다.

목록 (T)을 권할 수도 있습니다.

0

위의 코드는 dbnull 값이있는 경우 웹 응용 프로그램에서 작동하지 않습니다. 결과 목록 개체를 데이터 집합 또는 데이터 테이블로 변환해야합니다. 그것을위한 방법이 없습니다. 나는 같은 문제를 겪었으며 인터넷에서 여러 시간을 탐구 해본 결과, 해결책을 찾았으며 누구나이 문제를 해결할 수 있도록 여기에서 공유하고 싶다. 당신은 당신의 프로젝트에 클래스를 확인해야합니다 : - 여기

public class CollectionHelper 
    { 
     public CollectionHelper() 
     { 
     } 

     // this is the method I have been using 
     public DataTable ConvertTo<T>(IList<T> list) 
     { 
      DataTable table = CreateTable<T>(); 
      Type entityType = typeof(T); 
      PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType); 

      foreach (T item in list) 
      { 
       DataRow row = table.NewRow(); 

       foreach (PropertyDescriptor prop in properties) 
       { 
        row[prop.Name] = prop.GetValue(item) ?? DBNull.Value; 
       } 

       table.Rows.Add(row); 
      } 

      return table; 
     } 

     public static DataTable CreateTable<T>() 
     { 
      Type entityType = typeof(T); 
      DataTable table = new DataTable(entityType.Name); 
      PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType); 

      foreach (PropertyDescriptor prop in properties) 
      { 
       // HERE IS WHERE THE ERROR IS THROWN FOR NULLABLE TYPES 
       table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(
      prop.PropertyType) ?? prop.PropertyType); 
      } 

      return table; 
     } 
    } 

과 당신의 결정 보고서를 설정

CrystalReport1 cr1 = new CrystalReport1(); 

      var results = (from obj in context.tSamples 
          where obj.ID == 112 
          select new { obj.Name, obj.Model, obj.Producer }).ToList(); 
      CollectionHelper ch = new CollectionHelper(); 
      DataTable dt = ch.ConvertTo(results); 
      cr1.SetDataSource(dt); 
      crystalReportsViewer1.ReportSource = cr1;