0

我正在使用以下函数加载gridview以形成

private DataClasses1DataContext db_cooperations = new DataClasses1DataContext();
private int numberOfObjectsPerPage = 20;
private int CurrentPageIndex = 1;

Func<IEnumerable<cooperations>, IEnumerable<cooperations>> ordering = t => t.OrderBy(i => i.name);

private void daftarKoperasi_Load(object sender, EventArgs e)
{
    GetData(CurrentPageIndex);
    dataGridProperty();
}

 private void GetData(int page)
{
    page = page - 1;
    db_cooperations.Log = Console.Out;  

    var query = (from c in ordering(db_cooperations.cooperations)
                select new { c.id, c.name, c.phone, c.email, c.city })
                .Skip(numberOfObjectsPerPage * page).Take(numberOfObjectsPerPage).ToList();


    dataGridView1.DataSource = query ;
}

然后我想根据name搜索数据,同时过滤city。任何线索我应该进一步做什么?

更新

所以我更新我的代码如下

private void GetData(int page)
{
    page = page - 1;
    db_cooperations.Log = Console.Out;  

    var query = (from c in ordering(db_cooperations.cooperations)
                select new { c.id, c.name, c.phone, c.email, c.city })
                .Skip(numberOfObjectsPerPage * page).Take(numberOfObjectsPerPage).ToList();


    if (String.IsNullOrEmpty(searchTxt.Text) == false)
    {
        query.Where(c => c.name.ToLower().Contains(searchTxt.Text.ToLower()));
    }

    if (cityCB.SelectedIndex > -1)
        {
        query.Where(c => c.city.ToLower().Equals(cityCB.Text.ToLower()));
    }

    dataGridView1.DataSource = query ;
}

但它仍然不起作用

4

3 回答 3

1

在 linq 语句中添加 where 子句。

顺便说一句:您所有的排序、过滤和预测都在内存中完成。在应用任何这些之前,您正在将整个记录集加载到内存中,因为您使用IEnumerable<Corproations>的是IQueryable<Corporations>.

(from c in db_cooperations.cooperations 
where c.Name == name && c.City == city
order by c.Name)
.Skip(pagesize * page).Take(pagesize).AsEnumerable();
于 2013-01-27T16:28:29.653 回答
1

为了过滤(搜索)数据name,同时过滤Linq to Sqlcity中的数据。

您有使用or 运算符,其在 C#SQL中表示为|| 或, .Contains表示为。这是一个关于使用 LINQ 过滤的一个很好的例子,包含不区分大小写|LIKESQL.Contains

        var query = (from c in ordering(db_cooperations.cooperations)
                     select new { c.id, c.name, c.phone, c.email, c.city })
                     .where(c => c.name.ToLower().Contains(searchTxt.Text.ToLower()) || 
                            c => c.city.ToLower().Contains(searchTxt.Text.ToLower()))
            .Skip(numberOfObjectsPerPage * page).Take(numberOfObjectsPerPage).ToList();

PS:可能我会想念你的问题,但你会这样,

.where(c => c.name.ToLower().Contains(searchTxt.Text.ToLower()) && 
                                c => c.city == cityCB.Text)

编辑:

根据您的更新,

   if (String.IsNullOrEmpty(searchTxt.Text) == false)
    {
      query = query.Where(c => c.name.ToLower().Contains(searchTxt.Text.ToLower())).ToList();
    }

    if (cityCB.SelectedIndex > -1)
    {
       query =  query.Where(c => c.city.ToLower().Equals(cityCB.Text.ToLower())).ToList();
    }
于 2013-01-27T20:13:11.447 回答
0

所以我用下面的代码解决了这个问题

var query = (from c in ordering(db_cooperations.cooperations)
                         select new { c.id, c.name, c.phone, c.email, c.city })
                         .Skip(numberOfObjectsPerPage * page).Take(numberOfObjectsPerPage);

if (String.IsNullOrEmpty(searchTxt.Text) == false && searchTxt.Text.Trim() != "Search....")
    {
        query = from c in query
        where c.name.ToLower().Contains(searchTxt.Text.ToLower())
        select new { c.id, c.name, c.phone, c.email, c.city })
    }

if (kotaCB.SelectedIndex > -1)
    {
        query = from c in query
                where c.kota.ToLower().Equals(kotaCB.Text.ToLower())
                select new { c.id, c.name, c.phone, c.email, c.city });
        }

dataGridView1.DataSource = query.ToList();

谢谢大家帮助我@spajce,@Jason

于 2013-01-28T14:18:40.440 回答