0
\$\begingroup\$

I have a model:

class ColumnItsValues
{
    public int PropertyID { get; set; }
    public string ColumnName { get; set; }
    public string ValueOfColumn { get; set; }
}

two collections:

List<ColumnItsValues> listColumns = new List<ColumnItsValues>()
{
   new ColumnItsValues() {ColumnName="GUID", PropertyID=0, ValueOfColumn=null },
   new ColumnItsValues() {ColumnName="Name", PropertyID=1, ValueOfColumn=null },
   new ColumnItsValues() {ColumnName="Reference", PropertyID=2, ValueOfColumn=null },
   new ColumnItsValues() {ColumnName="Group", PropertyID=3, ValueOfColumn=null },
};

List<ColumnItsValues> listValues = new List<ColumnItsValues>()
{
    new ColumnItsValues() {ColumnName=null, PropertyID=1, ValueOfColumn="An user" },
    new ColumnItsValues() {ColumnName=null, PropertyID=2, ValueOfColumn="LIST of USERS" },
    new ColumnItsValues() {ColumnName=null, PropertyID=3, ValueOfColumn="False" },
    new ColumnItsValues() {ColumnName=null, PropertyID=0, ValueOfColumn=" " },
};

That's my linq query:

var fooColl = from columns in listColumns join values in listValues 
    on columns.PropertyID equals values.PropertyID
    select new { AColumn = columns.ColumnName, AValue = values.ValueOfColumn };

My question are:

  1. in my view, I should use where operator, but I use equals. Is it correct?
  2. Could you advice me more high-performance query?
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I would convert your fooColl to this:

IEnumerable<ColumnItsValues> foolCols = listColumns.Join(listValues,
            columns => columns.PropertyID,
            values => values.PropertyID,
            (columns, values) => new ColumnItsValues 
                                 {
                                     ColumnName = columns.ColumnName,
                                     ValueOfColumn = values.ValueOfColumn
                                  });

in this way you're using a type you already know (ColumnItsValues), which make it more extendable/testable/readable. About where == vs equals, I suggest to take a read to this blog post.

Alternatively, if as required by comments, you can change fooCols in this way:

IList<ColumnItsValues> foolCols = listColumns.Join(listValues,
            columns => columns.PropertyID,
            values => values.PropertyID,
            (columns, values) => new ColumnItsValues 
                                 {
                                     ColumnName = columns.ColumnName,
                                     ValueOfColumn = values.ValueOfColumn
                                  }).ToList();

or

ColumnItsValues[] foolCols = listColumns.Join(listValues,
            columns => columns.PropertyID,
            values => values.PropertyID,
            (columns, values) => new ColumnItsValues 
                                 {
                                     ColumnName = columns.ColumnName,
                                     ValueOfColumn = values.ValueOfColumn
                                  }).ToArray();

Depends on which additional functionalities you need and on your final purpose. My choice usually is try to keep my objects as simple and lightweight as possible. Both the array and the list extends IEnumerable<T>: IList<T> gives the capability to insert/remove at a given index and, since it is an extension of ICollection<T> permits you Add, Clear or check if it Contains an element, on the other hand if you keep using arrays (ColumnValues[]) the lenght would be immutable.

\$\endgroup\$
4
  • \$\begingroup\$ I cannot use IEnumerable<T> as I need access to elements by indexes. \$\endgroup\$
    – StepUp
    Dec 3, 2015 at 13:26
  • \$\begingroup\$ You can simply change that IEnumerable<ColumnItsValues> to ColumnItsValues[] and add a .ToArray() after the Join. I can change my answer to that if it helps you understanding what I mean. \$\endgroup\$ Dec 3, 2015 at 16:35
  • \$\begingroup\$ yeah, it will be really cool \$\endgroup\$
    – StepUp
    Dec 3, 2015 at 16:46
  • \$\begingroup\$ Indexing is a property of IList<T>, so .ToList() would work as well. \$\endgroup\$
    – Dan Lyons
    Dec 3, 2015 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.