如何使用C#linq在2个不同的表中获得2个不同的值[英] How to get 2 different values in 2 different tables using c# linq

本文是小编为大家收集整理的关于如何使用C#linq在2个不同的表中获得2个不同的值的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个具有dataGridView的C#程序 我想使用linq在2个不同的表中获得值 我知道我可以使用SQL Server存储的过程实现此目标 但是我想在LINQ中这样做,因此我不需要更新数据库

我的代码:

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;
}

另一个问题是,当我在保险范围中返回一个值时,系统会引发错误 因为我知道我将返回类型分配为列表(InsuranceHeader) 为达到这个?对不起,我的英语

推荐答案

您必须为其创建一个新类,因为方法无法返回匿名 type.

喜欢:

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;
}

我还将返回一个iEnumerable,因为此查询仅按需执行. Tolist()将迭代所有项目.如果您仅请求GetInsuranceList(1).FirstOfDefault()仅执行首先迭代. (除非您使用订单等)

其他推荐答案

我也有同样的问题,因此我使用了上一篇文章的解决方案. 不幸的是,我无法将其附加到我的GridView上. 在databind()上:

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

我得到了:

数据直接绑定到商店查询(dbset,dbquery,dbsqlquery)是 不支持.而是用数据填充DBSET,例如 调用DBSET上的负载,然后绑定到本地数据.对于WPF绑定 到dbset.local. winforms绑定到dbset.local.tobindinglist().

我在getInsurancelist()

的末尾添加了Tolist()
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;
}

现在我从databind()获得:

GridView的数据源没有任何属性或 生成列的属性

本文地址:https://www.itbaoku.cn/post/1556863.html

问题描述

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

推荐答案

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.)

其他推荐答案

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