简体   繁体   中英

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

I generate my classes with Linq to Sql, and i have a column " DataValue " which is a sql_variant .

MODEL

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

CONTROLLER :

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

And i used the View GENERATOR (Razor) To generate my LIST of Value from my QUERY.

Problem : I see all values, except Datavalue. There is nothing, and when i generate the view, By default, DataValue does not appear. I added it on my 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>
}

And there is no values. Can someone help me? I know that's an object property due to sql_variant , but, how to get a value from this ?

如本文所述,您必须实现一种变通方法以读取sql_variant类型: http : //www.codeproject.com/Articles/137509/Reading-sql-variant-in-Entity-Framework

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM