簡體   English   中英

如何使用LINQ TO SQL(ASP.NET)顯示sql_variant列中的值?

[英]How to show values from a sql_variant column with LINQ TO SQL (ASP.NET)?

我使用Linq to Sql生成類,並且具有“ DataValue ”列,它是sql_variant

模型

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_DataValue", DbType="Variant", UpdateCheck=UpdateCheck.Never)]
        public object DataValue
        {
            get
            {
                return this._DataValue;
            }

控制器:

public ActionResult getDataForAPlc()
        {
            int plc = 1; 
            var dataContext = new PLCDataContext();
            var datas = from tag in dataContext.PLC_Data_5m
                            where tag.TagID == plc && tag.TimeStampLocal >= new DateTime(2016, 7, 20) && tag.TimeStampLocal < new DateTime(2016,7,21)
                           orderby tag.TimeStampLocal descending
                           select tag;
            return View(datas);
        }

我使用了View GENERATOR (Razor)來從我的QUERY中生成我的值列表。

問題:我看到除Datavalue之外的所有值。 什么都沒有,並且當我生成視圖時,默認情況下,不會出現DataValue。 我在View.cshtml上添加了它

@model IEnumerable<DataReports.Models.PLC_Data_5m>

@{
    ViewBag.Title = "getDataForAPlc";
}

<h2>getDataForAPlc</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.TimeStampLocal)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DataTimeStamp)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DataQuality)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DataValue)
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.TimeStampLocal)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DataTimeStamp)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DataQuality)
        </td>
        <td>
           @Html.DisplayFor(modelItem => item.DataValue)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

而且沒有價值。 有人能幫我嗎? 我知道這是由於sql_variant而導致的對象屬性,但是,如何從中獲取值呢?

如本文所述,您必須實現一種變通方法以讀取sql_variant類型: http : //www.codeproject.com/Articles/137509/Reading-sql-variant-in-Entity-Framework

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM