Go Premium for a chance to win a PS4. Enter to Win

x
?
SolvedPrivate

Linq to SQL help

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Screencast - Getting to Know the Pipeline
Suggested Courses

885 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