CMinor
CMinor

Reputation: 427

How to get 2 different values in 2 different tables using c# linq

I have a c# program which has a datagridview I want to get values in 2 different tables using linq I know I can achieved this using sql server stored procedure But I want to do it in linq so I do not need to update database

Heres my code:

public List<InsuranceHeader> GetInsuranceList(int InsuranceHeaderId) 
{
    var getData =(from item in context.InsuranceHeader 
                  join item2 in context.InsuranceDetail
                  on item.InsuranceHeaderId equals item2.InsuranceDetailId
                  where item.InsuranceHeaderId == InsuranceHeaderId
                  select item).ToList();

    return getData;
}

The other problem is when im returning a value in InsuranceDetail The system throws an error because I know that I assigned my return type as a List(InsuranceHeader) is there anyway to achieve this? sorry for my english

Upvotes: 1

Views: 883

Answers (2)

user2818842
user2818842

Reputation: 25

I had the same problem, so I used the solution from previous post. Unfortunately, I couldn't attach it to my GridView. On DataBind():

 GridView1.DataSource = GetInsuranceList();
 GridView1.DataBind();

i got:

Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList().

I added ToList() at the end of GetInsuranceList()

public IEnumerable<InsuranceWithDetail> GetInsuranceList(int InsuranceHeaderId) 
{
    var results = (from item in context.InsuranceHeader 
                  join item2 in context.InsuranceDetail
                     on item.InsuranceHeaderId equals item2.InsuranceDetailId
                  where item.InsuranceHeaderId == InsuranceHeaderId
                  select new InsuranceWithDetail 
                  { 
                      InsuranceHeader = item, 
                      InsuranceDetail = item2 
                  }).ToList;

    return results;
}

Now I get from DataBind():

The data source for GridView did not have any properties or attributes from which to generate columns

Upvotes: 0

Jeroen van Langen
Jeroen van Langen

Reputation: 22083

You have to create a new class for it, because a method cannot return an Anonymous type.

like:

public class InsuranceWithDetail
{
    public InsuranceHeader InsuranceHeader { get; set; }
    public InsuranceDetail InsuranceDetail { get; set; }
}

public IEnumerable<InsuranceWithDetail> GetInsuranceList(int InsuranceHeaderId) 
{
    var results = from item in context.InsuranceHeader 
                  join item2 in context.InsuranceDetail
                     on item.InsuranceHeaderId equals item2.InsuranceDetailId
                  where item.InsuranceHeaderId == InsuranceHeaderId
                  select new InsuranceWithDetail 
                  { 
                      InsuranceHeader = item, 
                      InsuranceDetail = item2 
                  };

    // storing the results in a variable, will help on debugging. (quick watch)
    return results;
}

Also i would return an IEnumerable, because then the query is only executed on demand. The ToList() will iterate all items. If you only request GetInsuranceList(1).FirstOfDefault() only the first iteration is executed. (unless you use orderby etc.)

Upvotes: 3

Related Questions