Click here to Skip to main content
15,886,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
List<List<double>> Cust_Score_list = new List<List<double>>();           
            List<List<Dictionary<string, double>>> CustDict = new List<List<Dictionary<string, double>>>();

C#
string[] Name = {"Chandan","Thakurji","Sidhnath","Vinit","Guruji","Praveen"};

C#
for (int i = 0; i < Cust_Score_list.Count; i++)
         {
             //CustDict.Add(CustDict1);
            CustDict.Add( Cust_Score_list.Where(n => ConcateName(n, Name[i])).Select(g => g).ToList<Dictionary<string, double>>);
           //  var res = from n in CustDict1 where ConcateName(n,"chan") select n;

         }

C#
private List<Dictionary<Double, String>> ConcateName(List<double> Input, string Name)
       {
           Dictionary<Double, String> Result = new Dictionary<Double, String>();
           List<Dictionary<Double, String>> temp = new List<Dictionary<double, string>>();
           for (int i = 0; i < Input.Count; i++)
           {
            temp.Add((new Dictionary<double, string>(){ {Input[i], Name}}));              
           }
           return temp;

       }

But function ConcateName gives error in linq
C#
for (int i = 0; i < Cust_Score_list.Count; i++)
         {
             //CustDict.Add(CustDict1);
                CustDict.Add( Cust_Score_list.Where(n => ConcateName(n, Name[i])).Select(g => g).ToList<Dictionary<string, double>>();
}
Posted
Updated 21-Apr-14 3:48am
v2
Comments
Sergey Alexandrovich Kryukov 21-Apr-14 10:06am    
What are you trying to achieve?
—SA
rameshKumar1717 21-Apr-14 10:18am    
I want to convert list of type double to list of type dictionary where dictionary has key of type double and value has type string.
Sergey Alexandrovich Kryukov 21-Apr-14 10:31am    
What do you want to achieve by converting these lists?
—SA
Emre Ataseven 21-Apr-14 10:10am    
I'm sure there is a better way to implement what you try to do, it seems spooky.
rameshKumar1717 21-Apr-14 10:21am    
I know but I want to do it using LINQ.

If you can't fix it, rewrite it. This code is crap - sorry.
The naming is weird: CustDict is infact a List, you have a Result variable that is never used, naming a variable temp is almost always an indication that you don't know what it is (if you fail to give decent names, you are lost).


First advise: for nested containers: don't do it unless it's a very obvious case of a table - even there, I'd prefer proper custom classes instead of direct use of a container.

E.g. a table is best managed by rows of cells or columns of cells (and not list of list of cells):
C#
public class Table
{
    #region nested classes
    public class Cell
    {  
       public string Value { get; set; }
    }
    public class Row
    {
        private Cell[] _columns;

        // TODO: add argument checks: out-of-range (e.g. negative size)
        public Row(int cols) { _columns = new Cell[cols]; }
        // TODO: add argument checks: out-of-range 
        public Cell Column(int col) { return _columns[col]; }
        // convenience: row[col]
        public Cell this[in col] { get { return Column(col); } }
    }
    #endregion

    #region table
    private List<Row> _rows;

    public int NumOfColumns { get; private set; }
    public int NumOfRows { get { return _rows.Count; } }
    // TODO: add argument checks: out-of-range (e.g. negative size)
    public Table(int cols) { NumOfColumns = cols; _rows = new List<Row>(); }

    public void AddRow() { _rows.add(new Row(NumOfColumns)); }
    // TODO: add argument checks: out-of-range 
    public Row RowAt(int row) { return _rows[row]; }
    // convenience: table[row]
    public Row this[int row] { get { return RowAt(row); } }
    // convenience: table[row, col]
    public Cell this[int row, int col] { get { return this[row][col]; } }
    #endregion
}
etc.


With this warmup, second advise: Use composition instead of referring to common data via same index.

I.e. in your example you seem to refer to the same customer by same index over various collections. This is very error prone. Create a class that holds that information.

E.g.
C#
public class Customer
{
    public string Name { get; private set; }
    public Customer(string name) { Name = name; }
}
...
public class CustomerScores
{
    public Customer { get; private set; }
    public List<double> Scores { get; private set; }
    public CustomerScores(Customer customer)
    { Customer = customer; Scores = new List<double>(); }
    public void AddScore(double score) { Scores.Add(score); }
}



Third advise: create classes even for the tinyest piece of information (well, there is some limit, but you will get it for sure where to stop).

You seem to have some customer, some score and some containers that organize them in a certain manner (which is not clear to all of us according to the comments).

I make now an arbitrary choice that might not cover your example, but this is for illustration only.

C#
public class Customer       { ... } // from above
public class CustomerScores { ... } // from above
...
var customers = new List<Customer>()
{
    new Customer("..."),
    new Customer("..."),
    ...
};
var scoresPerCustomer = customers.Select(c=>new CustomerScores(c)).ToList();
...
// add scores...
...
// process scores (remove duplicates, sort, weight, ...?)
...
// make some reasonable use of the scores...
...


Now it's up to you to give sense to ConcateName(...) and CustDict etc.

Cheers
Andi
 
Share this answer
 
v3
Comments
Matt T Heffron 21-Apr-14 17:28pm    
+5!
Andreas Gieriet 21-Apr-14 18:31pm    
Thanks for your 5.
Cheers
Andi
C#
List<List<KeyValuePair<string, double>>> KeyList = new List<List<KeyValuePair<string, double>>>();
List<List<double>> Cust_Score_list = new List<List<double>>();

C#
for (int i = 0; i < Cust_Score_list.Count; i++)
         {
             List<KeyValuePair<string, double>> List1 = new       List<KeyValuePair<string, double>>();   
             List1 = Cust_Score_list[i].Select(g => new KeyValuePair<string, double>(Name[i], g)).ToList();
             KeyList.Add(List1);
         }

basically I am working on face recognition, I have list of list of double(name of list is Cust_Score_list) of different person, double values are distance of various nodal points or landmark points e.g width of left and right eye, length of nose, length of mouse, I wanted to attached name of each person along with their data. here is code in linq that helped me out to achieved that. Even I want to remove for loop if possible in future. KeyList will have final values in it. I have slightly large data so I could not paste it here.
 
Share this answer
 
Comments
Andreas Gieriet 22-Apr-14 4:58am    
Is this a solution or a comment to some questions? If it is a comment, remove this solution and post it as a response to some comment.
The code is still badly hacked, sorry. No reasonable abstraction in classes, no decent naming of anything. You seem to name things technically (*KeyList* does not tell anyone about the *logical* meaning of the construct, *List1* is as dumb as *temp* - sorry for stating it so blunt). I wonder how you maintain that in say two months from now...
You really need to improve in abstraction (create classes) and naming (give proper logical names that give the whole thing a meaning - e.g. *MyList* is not useful, while *AllCustomersWithTheirScores* makes thing clearer, etc.).
Andi
rameshKumar1717 22-Apr-14 5:38am    
Thanks for your valuable suggestion I have done sensible naming in project code, above code is just experimental one.
in your loop you have Name[i] and i is depend on number of items in Cust_Score_list . You need to make sure that Name list having equal or less than items compared to Cust_Score_list

next issue is you are calling CustDic.Add with List of items return from Linq. you may need to use AddRange method if you have multiple items to add ones

When You Filters a sequence of values based on a predicate (using where) that predicate should return boolean value. But your ConcateName return List instead of boolean
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900