SolvedPrivate

Linq to SQL help

Posted on 2013-12-03
5
45 Views
Last Modified: 2016-02-10
Please help.  I need to structure this and would like to leverage this as an example to do this with the best approach I can do with vs 2008.

I have a sql server database.  I have two tables.  To keep it simple lets just say table a and table b.  

We create a folder to hold all are entities.  In it for table a I have the following: lets assume this as table a.  Now the code for table b is the same.  Just for each data in the table.  So can or should I put more code here ..such as restraints...ect.  I ask this as my mind is in MVC and this is my first time doing asp.net since doing MVC4.  ok next is the controller we have or really just a bunch of business logic and linq code.  And other than coding better here is the root of my question.  Skip down to the next code segment to follow this question.  Basically I have two entities.  My controller code needs to reference a join from the other code in my linq query..but I cannot b/c it doesnt exist in the entity.  Here is the code for that.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace my.strong.name
{
    [DataContract]
    [Serializable]
    public class Brands
    {

        public Brands() { }

        [DataMember(Name = "BrandID")]
        public Int32 BrandID { get; set; }

        [DataMember(Name = "Name")]
        public String Name { get; set; }

        [DataMember(Name = "Description")]
        public String Description { get; set; }

        [DataMember(Name = "BusinessUnitID")]
        public Int32? BusinessUnitID { get; set; }
    }

}

Open in new window




Notice here that is uses the entity from above.  I am joining another table here.  BusinessUnitName exists in what I would refer to as table b.  Or another entity.  How do I get this in one statement.  I am confused.  Please help.
 public Brands[] GetBrands()
        {
           
               var query = from e in db.tblBrands
                           join bu in db.tblBusinessUnits on e.BusinessUnitID equals bu.BusinessUnitID                         
                           orderby e.BrandID
                           select new Brands
                           {
                               BrandID = e.BrandID,
                               Name = e.Name,
                               Description = e.Description,
                               BusinessUnitID = e.BusinessUnitID,
                               BusinessUnitName = bu.Description
                           };

            return query.ToArray();
            
        }       

Open in new window

0
Comment
Question by:Robb Hill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 39693317
not sure what are you trying to do here...
0
 
LVL 11

Author Comment

by:Robb Hill
ID: 39693329
two things ...but the major part of this is righting the linq code correct.


I am doing a join. in the code above between two tables and assigning it to a variable.  

                               BrandID = e.BrandID,
                               Name = e.Name,
                               Description = e.Description,
                               BusinessUnitID = e.BusinessUnitID,

This line of code crashes because BusinessUnitName does not exists in my entity brands.  I need to be able to do this..somehow.  I need these values populated from the database through linq...so they are accessable in my call from an asp.net datasource controll.  Do I modify the entity?  Do I modify this linq query...what do I do?

                               BusinessUnitName = bu.Description
0
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 39693448
you need to add BusinessUnitName to Brands

public class Brands
    {

        public Brands() { }

        [DataMember(Name = "BrandID")]
        public Int32 BrandID { get; set; }

        [DataMember(Name = "Name")]
        public String Name { get; set; }

        [DataMember(Name = "Description")]
        public String Description { get; set; }

        [DataMember(Name = "BusinessUnitID")]
        public Int32? BusinessUnitID { get; set; }

 [DataMember(Name = "BusinessUnitName")]
        public String BusinessunitName { get; set; }


    }
0
 
LVL 11

Author Comment

by:Robb Hill
ID: 39693701
so you are saying  its ok to add the datamember to that entity brands ..even though BusinessUnitName does not come from the table that it represents.
0
 
LVL 35

Expert Comment

by:YZlat
ID: 39695391
Sure, I have done that before. And it is not like BusinessUnitName is completely unrelated or anything. So that's one way of doing things. Another way would be create another entity and make BusinessUnitName a part of that second entity. Two entities, brands and Details could be then joined on BusinessUnitID and then query on that
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question