zoukankan      html  css  js  c++  java
  • 一个linq to sql 问题

    Model代码

    //唱片表

    [Table(Name="Album")]

    public class Album

    {

    //外键引用

    private EntityRef<Genre> _Genre;

    private EntityRef<Artist> _Artist;

     

    //构造方法

    public Album()

    {

    this._Artist = default(EntityRef<Artist>);

    this._Genre = default(EntityRef<Genre>);

    }

     

    [Column(IsPrimaryKey = true, Name = "AlbumId", DbType = "Int NOT NULL IDENTITY")]

    public int AlbumId { get; set; }

    [Column(Name="GenreId")]

    public int GenreId { get; set; }

    [Column(Name = "ArtistId")]

    public int ArtistId { get; set; }

    [Column(Name = "Title")]

    public string Title { get; set; }

    [Column(Name="Price")]

    public float Price { get; set; }

    [Column(Name = "AlbumArtUrl")]

    public string AlbumArtUrl { get; set; }

     

    [Association(Storage = "_Artist", ThisKey = "ArtistId")]

    public Artist Artist

    {

    get { return this._Artist.Entity; }

    set { this._Artist.Entity = value; }

    }

    [Association(Storage="_Genre",ThisKey="GenreId")]

    public Genre Genre

    {

    get { return this._Genre.Entity; }

    set { this._Genre.Entity = value; }

    }

    }

     

    //歌曲风格(流派)

    [Table(Name="Genre")]

    public class Genre

    {

    private EntitySet<Album> _Albums;

     

    public Genre()

    {

    this._Albums = new EntitySet<Album>();

    }

     

    [Column(IsPrimaryKey=true,Name="GenreId")]

    public int GenreId { get; set; }

    [Column(Name="Name")]

    public string Name { get; set; }

    [Column(Name = "Description")]

    public string Description { get; set; }

     

    [Association(Storage = "_Albums", ThisKey = "GenreId")]

    public EntitySet<Album> Albums

    {

    get { return this._Albums; }

    set { this._Albums.Assign(value); }

    }

    }

     

    //艺术家

    [Table(Name="Artist")]

    public class Artist

    {

    private EntitySet<Album> _Albums;

     

    public Artist()

    {

    this._Albums = new EntitySet<Album>();

    }

    [Column(IsPrimaryKey=true,Name="ArtistId")]

    public int ArtistId { get; set; }

     

    [Column]

    public string Name { get; set; }

     

    [Association(Storage = "_Albums", ThisKey = "ArtistId")]

    public EntitySet<Album> Albums

    {

    get { return this._Albums; }

    set { this._Albums.Assign(value); }

    }

    }

     

    //音乐商店

    public class MusicStoreModels:DataContext

    {

    public Table<Album> Albums;

    public Table<Genre> Genres;

    //默认构造方法

    public MusicStoreModels(string connection):base(connection){}

    }

    音乐风格Controller

    [HandleError]

    public class HomeController : Controller

    {

     

    public ActionResult Index()

    {

    //获得连接字符串

    string connStr = ConfigurationManager.ConnectionStrings["MusicStoreContext"].ConnectionString;

    //

    MusicStoreModels db = new MusicStoreModels(connStr);

     

    //获取10个流派

    var viewModel =

    (from g in db.Genres

    select g).Take(10);

     

    ViewData["Genres"] = viewModel;

    return View();

    }

    }

    音乐风格展示View

    <h2>音乐风格</h2>

    <ul>

    <%foreach (var gener in (IEnumerable<Genre>)ViewData["Genres"])

    {

    %><li><%=Html.ActionLink(gener.Name,"Borwse","Store",new{gener=gener.GenreId},new{Title=gener.Description}) %></li><%

    } %>

    </ul>

    唱片Controller

    public class StoreController : Controller

    {

    //

    // GET: /Store/

    // 某类音乐风格下的唱片

    public ActionResult Borwse()

    {

    //获取查询音乐分类

    int gener = Int32.Parse(Server.HtmlEncode(Request.QueryString["gener"]));

     

    //获得连接字符串

    string connStr = ConfigurationManager.ConnectionStrings["MusicStoreContext"].ConnectionString;

     

    MusicStoreModels db = new MusicStoreModels(connStr);

    string generName =

    (from g in db.Genres

    where g.GenreId == gener

    select g.Name).First();

     

    //查询某类音乐风格的唱片

    var albums =

    from a in db.Albums

    where a.GenreId == gener

    select a;

     

    ViewData["generName"] = generName;

    ViewData["album"] = albums;

    return View();

    }

    }

    唱片View

    下面的foreach里面的转换就提示: 转换无效

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

    <%@ Import Namespace="MvcApplication.Models" %>

    <h2>

    音乐分类:<%=ViewData["generName"]%></h2>

    <ul>

    <%foreach (var album in (IEnumerable<Album>)ViewData["album"])

    {%>

    <li><%=album.Title %></li>

    <%

    } %></ul>

    有点想不通,一样的代码.一样的类型.怎么就转换无效呢.

  • 相关阅读:
    Excel生成二维折线图详细教程 TheChosen
    sql语句截取字段中某一符号前几位的方法? TheChosen
    python快速下载包的镜像源? TheChosen
    Android 超大图长图浏览库 SubsamplingScaleImageView 源码解析
    记录线上APP一个排序比较引发的崩溃 Comparison method violates its general contract!
    Android 内存泄漏检测工具 LeakCanary(Kotlin版)的实现原理
    改数组长度
    枚举数组所有组合
    Ubuntu20.04启动后光标一直闪烁
    harbor镜像仓库清理操作
  • 原文地址:https://www.cnblogs.com/Dtscal/p/2549111.html
Copyright © 2011-2022 走看看