简体   繁体   中英

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);

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