Sangeeta
Sangeeta

Reputation: 388

how to loop through list returned by lambda expression?

this is my controller code:

    var myquery = mycontext.Websites.Select(x => new {x.pagetype,x.website1,x.creationdate,x.expirationdate,x.domainregistrar,x.area,x.pin
                    ,x.city, age = DateTime.Now.Subtract(x.expirationdate ?? DateTime.Today)
                });

return View(myquery);

and this is my view code:

@foreach (var item in Model)
    {
        <tr>
            <td>
                <span>@item.pagetype </span>

</td>

</tr>

}

how ever its giving me error: object does not contain definition of pagetype while traversing through foreach loop?

i wonder why?

EDIT:

I tried changing the code as below:

Controller:

   CRMDataContext mycontext = new CRMDataContext();
                var myquery = mycontext.Websites.Select(x => new WebsiteViewModel
                {
                    pagetype = x.pagetype,
                    site = x.site,
                    creationdate = x.creationdate ?? DateTime.Today,
                    expirationdate = x.expirationdate ?? DateTime.Today,
                    domain_registrar = x.domainregistrar,
                    Area = x.area,
                    pin = x.pin,
                    City = x.city,
                   difference = DateTime.Now.Subtract(x.expirationdate ?? DateTime.Today).ToString()
                }).ToList();

     return View(myquery);

but i'm getting exception:

Operation could destablize operation at runtime. Make sure your application is not loading two conflicting version of class library

all i can think..is linq generated class for Website table and my Websiteview model is conflicting . but i can not understand why?

Upvotes: 0

Views: 994

Answers (2)

Sangeeta
Sangeeta

Reputation: 388

After 1 whole day i could solve it out like below:

public WebSiteViewModel
{
     string PageType { get; set;}
     string WebSite { get; set;}
     DateTime CreationDate { get; set;}
     DateTime ExpirationDate { get; set;}
     string DomainRegistrar { get; set;}
     string Area { get; set;}
     string Pin  { get; set;}
     string City { get; set;}
     DateTime Age {get; set;}

     //Constructor
     public WebSiteViewModel(YourWebSiteEntity w)
     {
             PageType = w.pagetype;
             WebSite = w.website1;
             CreationDate = w.creationdate;
             ExpirationDate = x.expirationdate;
             DomainRegistrar = x.domainregistrar;
             Area = x.area;
             Pin = x.pin;
             City = x.city;
             Age = DateTime.Now.Subtract(x.expirationdate ?? DateTime.Today);
     }
}

controller:

public ActionResult Index()
        {
            CRMDataContext mycontext = new CRMDataContext();


            var myquery = mycontext.Websites.Select(x => new WebsiteViewModel(x.pagetype,
               x.site, x.creationdate ?? DateTime.Today, x.expirationdate ?? DateTime.Today, x.domainregistrar, x.pin, x.area, x.city, "Abc")).ToList();

            return View(myquery);
        }

View:

@model IEnumerable<ImportFromExcel.Model.WebsiteViewModel>
@{
    ViewBag.Title = "Index";
}
<h2>
    Index</h2>
<table>
    <tr>
        <th>
            Page type
        </th>
        <th>
            Website
        </th>
        <th>
            Creation date
        </th>
        <th>
            Expiration Date
        </th>
        <th>
        difference
        </th>
        <th>
            Domain Registrar
        </th>
        <th>
            Pin
        </th>
        <th>
            Area
        </th>
        <th>
            City
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                <span>@item.pagetype </span>
            </td>
            <td>
                <span>@item.site </span>
            </td>
..so on
        </tr>

    }
</table>

Hope some body else could find it useful as well. Problem was i was not using WebsiteViewModel Constructor to initiliaze model object . this solved my problem atlast. :)

Upvotes: 0

Tommy
Tommy

Reputation: 39807

To elaborate on the comment by MarcinJuraszek above, you need to create a view model class to hold these properties.

ViewModel class

public class MyViewModel{
   public string Pagetype {get;set;}
   public string Website1 {get;set;}
   public DateTime Creationdate {get;set;}
   public DateTime Expirationdate {get;set;}
   public string Domainregistrar {get;set;}
   public string Area {get;set;}
   public string Pin {get;set;}
   public string City {get;set;}
   public int Age {get;set;}
}

Now, in your query, select into the ViewModel class

var model = mycontext.Websites.Select(x => new MyViewModel{
                  PageType = x.pagetype
                  WebSite1 = x.website1
                  CreationDate = x.creationdate
                  ExpirationDate = x.expirationdate
                  DomainRegistrar = x.domainregistrar
                  Area = x.area
                  Pin = x.pin
                  City = x.city,
                  Age =DateTime.Now.Subtract(x.expirationdate ?? DateTime.Today)
               }).ToList();

return View(model);

And now, you need to strongly type your view and you will be good to go!

@model IEnumerable<MyViewModel>

@foreach (var item in Model)
    {
        <tr>
            <td>
                <span>@item.Pagetype </span>

</td>

</tr>

}

Upvotes: 2

Related Questions