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>

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

  • 相关阅读:
    调整JVM占用内存空间方法
    java基础1.5版后新特性 自动装箱拆箱 Date SimpleDateFormat Calendar.getInstance()获得一个日历对象 抽象不要生成对象 get set add System.arrayCopy()用于集合等的扩容
    java异常处理 throw RuntimeException时不需要同时方法中声明抛出throws 异常等待调用者catch进行捕获 子父类异常问题
    java自定义泛型 面试题:接收任意数组进行反转 泛型通配符
    javabeans 内省 introspector BeanUtils
    面试题:私有构造方法类外部能访问吗,用什么方法?反射
    java基础 java中枚举的应用 抽象方法问题
    【进阶修炼】——改善C#程序质量(7)
    【进阶修炼】——改善C#程序质量(6)
    【进阶修炼】——改善C#程序质量(5)
  • 原文地址:https://www.cnblogs.com/Dtscal/p/2549111.html
Copyright © 2011-2022 走看看