简体   繁体   English

使用LINQ搜索关系数据库

[英]Using LINQ to search relational databases

I have the following databases: 我有以下数据库: 在此处输入图片说明

and the following code: 和以下代码:

public IActionResult Index(IssueModel searchCriteria)
{
    var bloods = from m in _context.Blood
                 select m;
    if (!string.IsNullOrEmpty(searchCriteria.SearchComponent))
    {
        //Blood Component
        bloods = bloods.Where(s => s.Component.Contains(searchCriteria.SearchComponent));
        //Blood Type : This is where I plan to search for a specific blood type.
    }
    return View(bloods);
}

As you can see, I'm happy to search the blood database for a certain component type. 如您所见,我很高兴在血液数据库中搜索某种组分类型。 In the area I've marked I would like to search for blood that relates to a certain PlasType or RedBloodType. 在我标记的区域中,我想搜索与某个PlasType或RedBloodType相关的血液。

Any ideas? 有任何想法吗? Thanks! 谢谢!

Edit: Thanks to everyone who offered an answer. 编辑:感谢所有提供答案的人。 All the answers I've looked at have been incredibly useful not just in helping me crack this one, but improve my understanding of the topic. 我看过的所有答案不仅对帮助我破解这个问题非常有用,而且可以增进我对该主题的理解。

Like this:??? 像这样:???

    result = new List<Blood>();

    foreach (var b in bloods)
    {
      if (_context.Donor.Where(d => d.id == b.BloodId)
                        .Any(d => (d.PlasType == "somethingsYouNow") || (d.RedBloodType == "somethingsYouNow"))
      {
        result.Add(b)
      }
    }


    return result;

Don't you have the navigation property like Blood.Donors (if it's one to many relation)? 您没有像Blood.Donors这样的导航属性(如果是一对多关系)? You can access to it via this property but earlier you should include it: 您可以通过此属性访问它,但更早的时候应该包括它:

Blood.Include(c => c.Donors);

i would just create simple sql query and then convert it to linq query. 我只是创建简单的sql查询,然后将其转换为linq查询。

Select distinct blood.* from blood
inner join donor on blood.donorid = donor.donorid
where donor.PlasType like 'param' or RedBloodType like 'param'

now convert this to a linq query 现在将其转换为linq查询

var filteredBlood = (from b in blood
                      join d in donor on b.DonorId equals d.DonorId
                      where d.PlasType.contains("param") || d.RedBloodType.contains("param")
                      select b).distinct().toList();

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

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