英文:
I am lost with the Linq Shorthand IN clause
问题
"我已经搜索并找到了很多几乎可以理解的答案,但要么是我完全疯了,要么它们根本不适用于我的LINQ简写语法。我看过所有的“相似重复问题”...我也使用了Google。也许我只是不知道如何提问这个问题。
我的代码目前只返回3个城市,而不是城市表中的所有城市。我的简写对CityID == 1有效,但我不知道如何引用我插入的包含3个不同城市(1、2、3)的数组。如果我要使用CityID==1 || CityID==2 || CityID==3的语法,并且未来计划添加10-100个更多的城市,它将不会很好扩展。
public IList<Models.Cities> Cities { get;set; }
public async Task OnGetAsync()
{
Cities = (IList<Models.Cities>)await _context.tblCities.ToListAsync();
var citylist = new int[] { 1, 2, 4 };
Cities = Cities.Where(x => x.CityID==1).ToList();
}
重大修改/答案:
Svyatoslav Danyliv的解决方案有效,以下是我如何使用它。我可以堆叠各种计算或条件,逐一构建一小部分,而不是一个“工作或不工作”的巨大查询。容易删除或添加我组合的小部分。很多时候,这些Lambda表达式从发送给OnGetAsync()的内容中获取值。
public async Task OnGetAsync(string blah, int blahblah, int yadayada)
{
var citylist = new int[] { 1, 2, 4 };
var query = _context.tblCities.AsQueryable();
query = query.Where(c => citylist.Contains(c.CityID));
//一些计算在这里
query = query.Where(c => c.xxx == blah);
//一些计算在这里
query = query.Where(c => c.yyyy <= blahblah);
//在这里添加更多条件
query = query.Where(c => c.ooo = yadayada);
//最后打开Razor页面
Cities = await query.ToListAsync();
}
这会增加额外的开销吗?不太确定,由您来判断。但是在扩展时,我可以将所有不同的计算和条件放入一个巨大的查询中。当然,我可能会问Svyatoslav Danyliv,非常感谢!"
英文:
I have searched and found lots of almost understandable answers but either I am totally bonkers or they just don't fit in my LINQ Shorthand syntax. I looked at all the "similar duplicate questions" . . . I used The Google too. Maybe I just don't know how to ask the question.
My code to return only 3 cities for now out of a table of Cities. My shorthand works for CityID == 1 but I don't know how to reference the array I inserted with 3 different cities: 1,2,3. It would not scale very nice if I were to use the CityID==1 || CityID==2 || CityID==3 syntax and in the future planned to add 10-100 more cities.
public IList<Models.Cities> Cities { get;set; }
public async Task OnGetAsync()
{
Cities = (IList<Models.Cities>)await _context.tblCities.ToListAsync();
var citylist = new int[] { 1, 2, 4 };
Cities = Cities.Where(x => x.CityID==1).ToList();
}
BIG EDIT/ANSWER:
Svyatoslav Danyliv's solution worked and here is how I use it. I can stack all sorts of calculation or criteria building one piece at a time rather that one huge query that is a "work or doesn't". Easy to take out or add the little pieces I put together. Many times these Lambda expressions take values from what is sent to OnGetAsync().
public async Task OnGetAsync(string blah, int blahblah, int yadayada)
{
var citylist = new int[] { 1, 2, 4 };
var query = _context.tblCities.AsQueryable();
query = query.Where(c => citylist.Contains(c.CityID));
//SOME CALCULATIONS HERE
query = query.Where(c => c.xxx == blah);
//SOME CALCULATIONS HERE
query = query.Where(c => c.yyyy <= blahblah);
//ADD MORE HERE
query = query.Where(c => c.ooo= yadayada);
//AND FINALLY OPEN THE RAZOR PAGE
Cities = await query.ToListAsync();
}
Does this increase the overhead? Not sure, you tell me. But when it comes time to scale I could put all the different calculations and criteria in to one honking huge query. Of course I will probably ask Svyatoslav Danyliv thank you very much!
答案1
得分: 1
筛选应在使用 Contains
进行材料化之前完成。
public async Task OnGetAsync()
{
var citylist = new int[] { 1, 2, 4 };
var query = _context.tblCities.AsQueryable();
if (citylist.Length > 0) // 条件查询
query = query.Where(c => citylist.Contains(c.CityID));
Cities = await query.ToListAsync();
}
英文:
Filtering should be done before materialization using Contains
.
public async Task OnGetAsync()
{
var citylist = new int[] { 1, 2, 4 };
var query = _context.tblCities.AsQueryable();
if (citylist.Length > 0) // conditional querying
query = query.Where(c => citylist.Contains(c.CityID))
Cities = await query.ToListAsync();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论