简体   繁体   English

给出多个清单 <int> 在实体框架中使用linq键入条件

[英]Giving Multiple List<int> type criteria by using linq in Entity Framework

I am working a project that uses entity framework. 我正在使用一个使用实体框架的项目。 I want simple thing when people click to searchLookUpedit button I want to show values filtered according to Companies that exists in Orders. 当人们点击searchLookUpedit按钮时我想要简单的事情我想显示根据Orders中存在的公司过滤的值。 So here is the code: 所以这是代码:

private void SearchLookUpEdit_Customer_Click(object sender, EventArgs e)
{
    object [] siparisNo = new object[gridView1.RowCount];
    List<Siparisler> siparisList = new List<Siparisler>();
    List<int> firmaIds = new List<int>();

    for (int i = 0; i < gridView1.RowCount; i++)
    {
        siparisNo[i] = gridView1.GetRowCellValue(i,"SiparisNo");
        int sipNo = Convert.ToInt32(siparisNo[i]);
        Siparisler siparis = context.Siparisler.Where(p => p.Id == sipNo).FirstOrDefault();
        siparisList.Add(siparis);
        firmaIds.Add(siparis.Firma_Id);
    }

    for (int i = 0; i < firmaIds.Count; i++)
    {
        int a = firmaIds[i];
        firmalarBindingSource.DataSource = context.Firmalar.Where(p => p.Id == );
    }
}

In here second for loop . 在这里第二个循环。 Lets imagine that in firmaIds<int> list type have 3 values. 让我们假设在firmaIds<int>列表类型中有3个值。 And assume that they are 3, 5 and 8 for example, and I want only this 3 Companies will exist in firmalarBindingSource.DataSource after the click event finished running. 并假设它们是3,5和8,例如,我想只有这3个公司将在click事件完成运行后存在于firmalarBindingSource.DataSource I tried but it did not. 我试过但没有。 If my criteria were different data type it was easy to filter. 如果我的标准是不同的数据类型,则很容易过滤。 Is there anyway to do this? 反正有没有这样做?

If I have understood what you are asking try replacing 如果我理解了你的要求,请尝试更换

 for (int i = 0; i < firmaIds.Count; i++)
 {
     int a = firmaIds[i];
     firmalarBindingSource.DataSource = context.Firmalar.Where(p => p.Id == );
 }

with

firmalarBindingSource.DataSource = context.Firmalar.Where(p => firmaIds.Contains(p.Id));

@Faby answered your question, but I just wanted to add that you can also optimize the first part of your code, so you could do everything in two lines of code in a more functional manner using Linq : @Faby回答了你的问题,但我只想补充一点,你也可以优化代码的第一部分,这样你就可以使用Linq以更多功能的方式完成两行代码中的所有操作:

IEnumerable<Firmalar> firmalarDataSource = Enumerable.Range(0, gridView1.RowCount - 1)
    .Select((index) =>
    {
        var siparisId = Convert.ToInt32(gridView1.GetRowCellValue(index, "SiparisNo"));
        var siparis = context.Siparisler.FirstOrDefault(p => p.Id == siparisId);
        return context.Firmalar.FirstOrDefault(f => f.Id == siparis.Firma_Id);

    })
    .Distinct();

firmalarBindingSource.DataSource = firmalarDataSource;

Note : these are two lines, but i adjusted the formatting to be more readable ;) 注意 :这些是两行,但我调整格式更具可读性;)

If you value performance over lines of code, here is a three line example with less roundtrips to the DB: 如果您重视代码行的性能,这里是一个三行示例,其中DB的往返次数较少:

var siparisIds = Enumerable.Range(0, gridView1.RowCount - 1)
    .Select(index => Convert.ToInt32(gridView1.GetRowCellValue(index, "SiparisNo")));

var firmaIds = context.Siparisler.Where(p => siparisIds.Contains(p.Id)).Select(s => s.Firma_Id).Distinct();

firmalarBindingSource.DataSource = context.Firmalar.Where(f => firmaIds.Contains(f.Id));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM