SolvedPrivate

Linq to SQL help

Posted on 2013-12-03
5
42 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

740 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