使用Linq在3d数组中搜索 [英] Searching in 3d arrays using Linq

查看:71
本文介绍了使用Linq在3d数组中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我有这样的模特:

 public enum MainKeys 
{
A,B,C, D,E,F
}


公共枚举SecondKeys
{
Q,W,E,R,T,Y
}

public static ConcurrentDictionary< MainKeys,Model2> PrimaryList = new ConcurrentDictionary< MainKeys,Model2>();

公共类Model1
{
public string Name {get; set;};

public ConcurrentDictionary< int,Model2> SecondLevelList = new ConcurrentDictionary< int,Model2>();
}

公共类Model1
{
public string SubName {get; set;};

public ConcurrentDictionary< SecondKeys,Model3> SecondLevelList2 = new ConcurrentDictionary< SecondKeys,Model3>();
}

公共类Model3
{
public TextBlock TextInstance {get; set;} = null;
}

现在我需要搜索是否有人的TextInstance不为空。

 

public void start(MainKeys mk,SecondKeys sk)
{

if(PrimaryList [mk] .SecondLevelList2.Any(st => st) .SecondKeys [sk] .TextInstance!= null)== true)

{

Debug.WriteLine(" found");

}

否则

{的Debug.WriteLine(QUOT; NOTFOUND");

}

}


问题是它总是失败,我在这里缺少什么?它失败了,FirstOrDefault,这是否意味着它无法在int类型concurentdictiony中搜索,我该如何解决呢?


解决方案

您发布的代码无法编译。您似乎有两种类型与Model1相同的类型。我假设第二个是Model2。似乎根本没有使用Model1。你有分号,你不应该。此代码无法编译。
您是否从工作代码中复制了此代码或手动输入?也许你的代码没有编译(因此不能正常工作),所以你仍然在运行旧版本。


你也没有指定你在PrimaryList中实际拥有的输入有点难以看到你想要做什么。在并发字典中有并发字典这一事实对我来说似乎是一个设计问题。在你必须经历所有锁定的情况下,性能变得很慢。


我将尝试分开选择此代码以查看问题可能存在的位置。 PrimaryList是一个字典。您会发现Model2与传递给函数的MainKeys相匹配(如果有)。然后,您枚举SecondLevelList2集合。在这里你看任何Model3的
,其中SecondKeys字典包含SecondKeys,如果有的话,TextInstance不为null。 Model3中没有SecondKeys属性。同样,这段代码无法编译。只是猜测,但我假设你想在SecondLevelList2
中找到任何具有SecondKeys条目且TextInstance不为null的条目。但是你的代码都没有处理字典可能没有这些值的事实,你是否保证所有字典总是拥有MainKeys和SecondKeys的所有实例?如果不是你的代码
将抛出一个异常。


这是你的if代码的修改版本做同样的事情。因为这是一个你不需要使用Any的字典,所以有0或1个实例。请注意,我没有检查是否存在键,因为你不是,但是你需要在尝试使用它们之前验证mk和sk值是否在字典中。

 var model = PrimaryList [mk] .SecondLevelList2 [sk]; 
if(model!= null&& model.TextInstance!= null)


回到我的原来,我认为你的代码中存在设计缺陷。您无法弄清代码中的各种嵌套级别这一事实表明存在任何问题。你真的需要重新考虑你想要存储的内容并找出一个更好的方法来存储它,因为这个代码会变得更加复杂并且不会更容易维护。


Hello I have such model:

public enum MainKeys
{
A,B,C,D,E,F
}


public enum SecondKeys
{
Q,W,E,R,T,Y
}

public static ConcurrentDictionary<MainKeys,Model2> PrimaryList = new ConcurrentDictionary<MainKeys,Model2>();

public class Model1
{
  public string Name{get;set;};

  public ConcurrentDictionary<int,Model2> SecondLevelList = new ConcurrentDictionary<int,Model2>();
}

public class Model1
{
  public string SubName {get;set;};

  public ConcurrentDictionary<SecondKeys,Model3> SecondLevelList2 = new ConcurrentDictionary<SecondKeys,Model3>();
}

public class Model3
{
  public TextBlock TextInstance {get;set;} = null;
}

Now I need to search if anyone has TextInstance not null.

public void start(MainKeys mk, SecondKeys sk) {

if(PrimaryList[mk].SecondLevelList2.Any(st => st.SecondKeys[sk].TextInstance != null) == true)

{

Debug.WriteLine("found");

}

else

{Debug.WriteLine("notfound");

}

}

Problem is it always fails, what I'm missing here? It fails with any, FirstOrDefault, does that mean it fails to search in int type concurentdictionaty, how can I solve it?

解决方案

The code you posted doesn't compile. You seem to have 2 types with the same name of Model1. I assume the second one is Model2. Model1 doesn't seem to be used at all. You have semicolons where you shouldn't. This code cannot possibly compile at this point. Did you copy this code from working code or type it in by hand? Maybe your code isn't compiling (and hence isn't working) so you're still running the old version.

You also didn't specify what input you actually had in PrimaryList so it is a little hard to see what you're trying to do. The fact that you have concurrent dictionaries within concurrent dictionaries seems like a design issue to me. Performance is going to be slow with all the locking you have to be experiencing.

I'm going to try to pick this code apart to see where the problem could reside. PrimaryList is a dictionary. You find the Model2 that matches the MainKeys passed to the function, if any. You then enumerate the SecondLevelList2 collection. Here you are looking for any Model3 that's SecondKeys dictionary contains SecondKeys, if any and that TextInstance is not null. There is no SecondKeys property in Model3. Again, this code cannot compile. Just guessing but I assume you want to find any entries in SecondLevelList2 that has a SecondKeys entry and that TextInstance is not null. But none of your code handles the fact that the dictionary may not have such values, are you guaranting that all dictionaries always have all instances of MainKeys and SecondKeys? If not your code will throw an exception.

Here's a modified version of your if code that does the same thing. Because this is a dictionary you don't need to use Any, there is either 0 or 1 instance. Note that I'm not checking for the existence of the key to begin with because you weren't but you really should verify the mk and sk values are in the dictionary before trying to use them.

var model = PrimaryList[mk].SecondLevelList2[sk];
if (model != null && model.TextInstance != null)

Going back to my original point, I think you have a design flaw in your code. The fact that you cannot figure out the various nesting levels in your code indicates any issue. You really need to reconsider what you're trying to store and figure a better way of storing it because this code is just going to get more complicated and it won't get any easier to maintain.


这篇关于使用Linq在3d数组中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆