Question

How can I use LINQ to select all the Company Name and Company ID from all the rows? I need something like this pseudo-code:

 var typedQry = from b in allData.AsEnumerable()
                where b.GetHeader("xxx") == "08/10/09 to 08/26/09"
                select CompanyName, CompanyID, ...

The code below selects only one Company Name. Instead, I want Company Name from all the rows:

var typedQry3  = from b in allData.AsEnumerable()
select new { compname0 = b._rows[0][5]}; 

The data in _rows are Company Name (e.g., allData[0]._rows[0][5], allData[0]._rows[1][5],....), Company ID, and so forth.

However, Company Name, Company ID, and etc. are not defined in the DataProperty class. Their values are inserted into _rows from data files.

Any help is appreciated. Below is some code to help you understand my question.

List<DataProperty> allData = new List<DataProperty>();

The DataProperty class consists of

private readonly Dictionary<string, string> _headers = new Dictionary<string, string>(); 
private readonly List<string[]> _rows = new List<string[]>();

and these methods (among others):

public string[] GetDataRow(int rowNumber){return _rows[rowNumber];}
public void AddDataRow(string[] row){_rows.Add(row);}
Était-ce utile?

La solution 2

you can use this to get all company names:

var AllCompanyNames = allData.SelectMany(u => u._rows.Select(t => t[5])).ToList();

and this, to get more property:

var Rows = allData.SelectMany(u => 
        u._rows.Select(t => new
        {
            CompanyName = t[5],
            Other1 = t[1],
            Other2 = t[2]
        }))
    .ToList();

and this, if you need to check any condition:

var FilteredRows = allData.SelectMany(u => 
        u._rows.Select(t => new
        {
            CompanyName = t[5],
            Other1 = t[1],
            Other2 = t[2]
        }))
    .Where(u => u.CompanyName == "XXX")
    .ToList();

Autres conseils

according to your comment, if you need to the sum for each company you can try this:

var RowList1 = allData.SelectMany(u => u._rows.Select(t => new
        {
            CompanyName = t[5],
            Amount = Convert.ToInt64(t[1]) + Convert.ToInt64(t[2])
        }))
    .Where(u => u.CompanyName == "XXX")
    .OrderBy(u => u.CompanyName)
    .ToList();

and if you need to sum of the all companies, you can try this:

var SumAmount = allData.SelectMany(u => u._rows.Select(t => new
        {
             CompanyName = t[5],
             Amount = Convert.ToInt64(t[1]) + Convert.ToInt64(t[2])
        }))
    .Where(u => u.CompanyName == "XXX")
    .DefaultIfEmpty()
    .Sum(u => u.Amount);

you can write your own and customized query using these

At first you can receive rows and then iterate through them. This example may help you

var rows = (from DataRow dRow in dTable.Rows
                            select new {col1=dRow["dataColumn1"],col2=dRow["dataColumn2"]});

        foreach (var row in distinctRows) 
        {
            var value1=row.col1.ToString();
            var value2=row.col2.ToString();  

        }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top