简体   繁体   English

IEnumerable和IQueryable的联合

[英]Union of IEnumerable and IQueryable

// get cats out of local db
var localDb = db.Categories.Select(c => c.Name);
// get cats out of wcf
var wcf = service.Categories().Select(c => c.CatName);

// create union set
var all = new HashSet<String>(localDb);
all.UnionWith(wcf);

The above code works fine, but the code below throws a runtime error. 上面的代码工作正常,但是下面的代码引发运行时错误。

var localDb = db.Products.Where(c => c.Category.Equals(name))
                         .Select(p => p.Name);

var wcf = service.Products().Where(c => c.CategoryId == 
                             service.CategoryByName(name).CategoryId)
                            .Select(p => p.ProName);

var all = new HashSet<String>(localDb);
all.UnionWith(wcf);

exception: 例外:

An exception of type 'System.ArgumentException' occurred in 
System.Data.Entity.dll but was not handled in user code
Additional information: DbComparisonExpression requires arguments with 
comparable types.

Can anyone explain why the first one works and the second doesn't? 谁能解释为什么第一个有效而第二个无效?

This line: 这行:

var localDb = db.Products.Where(c => c.Category.Equals(name))
                .Select(p => p.Name);

tries to compare names (which I expect are strings) with category objects (which I expect are not) 尝试将名称(我期望是字符串)与类别对象(我期望不是)进行比较

You can't compare the two, so you get an error. 您无法将两者进行比较,因此会出现错误。

From your previous example I suspect you wanted to write 从上一个示例中,我怀疑您想写

var localDb = db.Products.Where(c => c.Category.Name.Equals(name))
                .Select(p => p.Name);

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

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