Reputation: 13769
My program should read and process data from legacy flat files.
I read the data into private ICollection<transaction> transactions;
public class transaction
{
public int transactionNum;
public DateTime transactionDate;
public int orderNum;
public string customerName;
public Decimal amount;
}
How do I create a DataSource
to this ICollection<T>
(or a LINQ query of it), so that I can DataBind
several control values to it?
Note: I have minimal LINQ knowledge. In the past, I could just drag a database table into my application.
Upvotes: 1
Views: 154
Reputation: 460278
From your comment:
On the ASP.NET form, I have a CheckBoxList named customerCheckL. Upon loading the data file, the code should populate customerCheckL.Items with the list of distinct customer names. How do I do that? customerCheckL.DataSourceID = ???
That makes more sense. You could implement an EqulityComparer<transactions>
class which compares by customer:
public class TransactionCustomerComparer : IEqualityComparer<transaction>
{
public bool Equals(transaction x, transaction y)
{
if (x == null || y == null) return false;
return x.customerName == y.customerName;
}
public int GetHashCode(transaction obj)
{
if (obj == null) return int.MinValue;
return obj.customerName.GetHashCode();
}
}
(Note that you can use this method in all Enumerable
methods that allow to pass a custom comparer)
Then you can use Distinct
to get a unique list. You just have to set the DataSource
, DataTextField
and DataValueField
and DataBind
the CheckBoxList
.
var customerComparer = new TransactionCustomerComparer();
customerCheckL.DataSource = transactions.Distinct(customerComparer);
customerCheckL.DataTextField = "customerName";
customerCheckL.DataValueField = "transactionNum";
customerCheckL.DataBind();
Upvotes: 1