开发者

Entity Framework: Name Collisions (.NET MVC)

开发者 https://www.devze.com 2023-01-13 03:15 出处:网络
extremely sorry for writing here, I\'m a php guy, been doing .NET MVC for like three days, although I had experience with C# some time ago. Anyways, I\'m doing a simple website with shops and news sto

extremely sorry for writing here, I'm a php guy, been doing .NET MVC for like three days, although I had experience with C# some time ago. Anyways, I'm doing a simple website with shops and news stories. A news story could be either a global one, or tied to some shop. I use ShopId in the News table which can be 0.

What I'm trying to achieve is get the news which are not tied to any shop, and then in a different section get those tied to shops (where ShopId > 0) and I'd like to list them in a nice format, like: Shop Na开发者_开发问答me -- News heading, where the shop name would lead to the shop's page, and the news heading would lead to the news page.

The problem is that both Shops and News have fields called Name and Slug. I'm using the following code to retrieve the news alone:

var news = db.News.Include("User").Where(s => s.ShopId == 0)
  .OrderByDescending(d => d.PublishDate).ToList();

Which seems to work fine. My method for retrieving news together with shops is currently the following:

var shopsNews = db.News.Include("User").Include("Shop").Where(s => s.ShopId > 0)
  .OrderByDescending(d => d.PublishDate).ToList();

In order to understand the structure of my database I posted it to Twitpic: http://twitpic.com/2im4xm

Any help would be appreciated. Thanks! ~ K


var q = from n in db.News
        orderby n.PublishDate desc
        select new NewsPresentation // view model class you write
        {
            ShopName = n.ShopId == 0 ? "No shop, sorry!" : n.Shop.Name,
            NewsName = n.Name,
            UserName = n.User.Name,
            // etc.
        };
0

精彩评论

暂无评论...
验证码 换一张
取 消