Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the below code TemplateEntityList contains list of templateentity.
after all coding lstTemplateEntity binds only first template entity data.it is not containg all records.
please help.

C#
var TemplateEntityList = objrestlistinfo.RESTInfoResponseList.Select(x => x.TemplateEntityList).Distinct();
           List<template>    lstTemplateEntity = new List<template>();
                foreach (var templatesetset in TemplateEntityList.ToList())
                {
                    Template obj = new Template();
                    obj.TemplateID = templatesetset[0].TemplateID;
                    obj.TemplateName = templatesetset[0].TemplateName;
                    lstTemplateEntity.Add(obj);
                }
Posted
Updated 18-Nov-13 19:03pm
v2
Comments
Ron Beyer 19-Nov-13 1:15am    
How many entries are in the "TemplateEntityList" object? I'm guessing you only have one object being returned from your LINQ query.
connect2manas 19-Nov-13 1:28am    
TemplateEntityList contains 14 list.so templatesetset .count() also contains 14 list.
Er Daljeet Singh 19-Nov-13 1:29am    
the code you had posted is correct but you need two modification.
first check whether the record you are having the table are distinct of not in your case i think you have multiple TemplateEntityList in the table.
second you should modify your foreach loop

Template obj = new Template();
obj.TemplateID = templatesetset[0].TemplateID;
obj.TemplateName = templatesetset[0].TemplateName;
lstTemplateEntity.Add(obj);

here in this code you had put zero so it will always read first record only.
connect2manas 19-Nov-13 1:42am    
You are right.
TemplateEntityList .count() has 1 record.
But templatesetset .count() has 14 records.
so templatesetset[0].TemplateID=101;
templatesetset[1].TemplateID=200;
templatesetset[3].TemplateID=899;
as on display.
so how can i iterate all the values .please help me.
Er Daljeet Singh 19-Nov-13 1:50am    
can you share the column of your table.

Hi Manas,

You can try this code
XML
var TemplateEntityList = objrestlistinfo.RESTInfoResponseList.Select(x => x.TemplateEntityList).Distinct();
               List<template>    lstTemplateEntity = new List<template>();
int indx = 0;                    
foreach (var templatesetset in TemplateEntityList.ToList())
                    {
                        Template obj = new Template();
                        obj.TemplateID = templatesetset[indx].TemplateID;
                        obj.TemplateName = templatesetset[indx].TemplateName;
                        lstTemplateEntity.Add(obj);
indx++;
                    }

I think you are trying to move first element alone to the object, so I added index value to get respective data from the table and move to obj.

Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
v2
Comments
connect2manas 19-Nov-13 1:47am    
Hi RK,
Actually TemplateEntityList .count() has 1 record.
But templatesetset .count() has 14 records.
so looping happens only once.
♥…ЯҠ…♥ 19-Nov-13 1:49am    
So you have found the root cause for the problem?
Er Daljeet Singh 19-Nov-13 1:53am    
i think here "templatesetset .count() has 14 records." is returning you the number of columns in you table.
connect2manas 19-Nov-13 2:02am    
i have a nested list class like as below
public class RestListInfo
{
public int ErrorCode { get; set; }
public string ErrorMessage { get; set; }
[XmlElement("RESTListInfoResponse")]
public List<restlistinforesponse> RESTInfoResponseList = new List<restlistinforesponse>();
}

public class RESTListInfoResponse
{
[XmlElement("TemplateEntity")]
public List<template>
XML
i have a nested list class like as below
 public class RestListInfo
    {
        public int ErrorCode { get; set; }
        public string ErrorMessage { get; set; }
        [XmlElement("RESTListInfoResponse")]
        public List<restlistinforesponse> RESTInfoResponseList = new List<restlistinforesponse>();
    }

    public class RESTListInfoResponse
    {
        [XmlElement("TemplateEntity")]
        public List<template> TemplateEntityList = new List<template>();


    }

    public class Template
    {
        public int TemplateID { get; set; }
        public String TemplateName { get; set; }
        public String TemplateDescription { get; set; }

    }

i got the list as
  var TemplateEntityList = objrestlistinfo.RESTInfoResponseList.Select(x =&gt; x.TemplateEntityList).Distinct();
 
Share this answer
 
v2

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