크리스탈 보고서 및 LINQ
-
01-07-2019 - |
문제
은 사람을 알아 냈을 사용하는 방법 크리스탈과 함께 보고서 Linq to SQL?
해결책
변환할 수 있습 LINQ 결과로 설정 List
, 당신이 필요하지 엄격하게 사용 DataSet
으로 보고서 SetDataSource
, 할 수 있습을 공급 크리스탈 보고서와 데이터는 IEnumerable
.이 List
에서 상속 IEnumerable
당신은 설정할 수 있습니다 당신의 보고서는'데이터 소스 목록에,당신은 전화 .ToList()
는 방법에 LINQ 결과를 설정합니다.기본적으로:
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;
다른 팁
이 msdn doc's 을 제안할 수 있는 바인딩 크리스탈 보고서를 ICollection.
수도 내가 목록을 추천(T)?
비록 나는 그것을 시도하지 않은 나 자신이 될 것으로 보인 가능한의 조합을 사용하여 매핑되는.LoadOptions 게 열심을 받아들이 홍보 및 GetCommand(진행)을 반환한다.체를 보존하는 관계입니다.
더 많은 정보를 참조하십시에 MSDN 포럼.
위의 코드 작업에서 웹 응용 프로그램이 있는 경우 이 변환은 지원 값입니다.을 변환하는 결과 목록 데이터 집합체 또는 datatable.이 없는 방법에 지어진다.라과 같은 문제 후에는 시간의 탐험,인터넷에서 나는 해결책을 발견하고 공유하고 싶어 사람을 돕기 위해 여기에 붙어있습니다.당신은 클래스에서는 프로젝트:-
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;
제휴하지 않습니다 StackOverflow