【问题标题】:How to show values from a sql_variant column with LINQ TO SQL (ASP.NET)?如何使用 LINQ TO SQL (ASP.NET) 显示 sql_variant 列中的值?
【发布时间】:2016-07-22 10:57:35
【问题描述】:

我使用 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,我知道这是一个对象属性,但是,如何从中获取值?

【问题讨论】:

    标签: c# asp.net asp.net-mvc linq-to-sql sql-variant


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      • 2016-02-04
      • 1970-01-01
      • 2014-06-22
      相关资源
      最近更新 更多