如何遍历列表中的lambda前pression回来? [英] how to loop through list returned by lambda expression?

查看:109
本文介绍了如何遍历列表中的lambda前pression回来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的控制器code:

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);

这是我的看法code:

and this is my view code:

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

</td>

</tr>

}

怎么过它给我的错误:对象不包含网页类型的定义,同时通过foreach循环遍历

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

我不知道为什么?

编辑:

我试图改变code如下:

I tried changing the code as below:

控制器:

   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);

但我得到异常:

操作可以在运行时destablize操作。请确保您的应用程序没有加载两个相互矛盾的类库版本

所有我能为网站桌子和我Websiteview模型think..is LINQ生成的类是相互矛盾的。但我不明白为什么?

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

推荐答案

在1整天的时间,我可以解决它象下面这样:

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);
     }
}

控制器:

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);
        }

查看:

@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>

希望一些身体还能发现它也是有用的。问题是我没有使用WebsiteViewModel构造以initiliaze模型对象。这个解决我的问题atlast。 :)

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. :)

这篇关于如何遍历列表中的lambda前pression回来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆