简体   繁体   中英

How to use array value in Entity Framework in “where”

I have one array I want to use array value in where Entity Framework

List<string> arrayN = new List<string>();
for (int i = 0; i <= 100; i++) {
     arrayN.Add(i);
}

I want to use the array in her database Entity Framework

var result = (from s in _entity.ArdSamaneh
                   where arrayN.Contains(s.Code.Trim().ToString())
                   select new {
                   name = s.NameFamily,
             }).ToList();
dataGridView1.DataSource = result;

You could set the "let" clause to defined the array into the query, this example helps to visualize the implementation

var data = new[] {
    new { Id=1, Name= "Name 1" },
    new { Id=2, Name= "Name 2" },
    new { Id=3, Name= "Name 3" },
    new { Id=4, Name= "Name 4" },
    new { Id=5, Name= "Name 5" },
    new { Id=6, Name= "Name 6" },
    new { Id=7, Name= "Name 7" },
    new { Id=8, Name= "Name 8" }
};
Console.WriteLine(data);

var filterIds = new[] {2,5,8};

var filteredData = from d in data
                    let ids = filterIds
                    where ids.Contains(d.Id)
                    select d;

Console.WriteLine(filteredData);

Hope it helps - M.Acosta.D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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