Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
SolvedPrivate

Linq to SQL help

Posted on 2013-12-03
5
Medium Priority
?
46 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 2000 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

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

704 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