简体   繁体   中英

Comparison operators not support for 'System.String[]' in a Linq query

I'm working on a Linq query to join data from two tables (using Linq to SQL), with the logic as follows:

  • Banners contains a field which has comma separated values in it. I want to split this column and have a list of IDs (for example 1,2,3,4)
  • References contains a list of these mappings with 1:1 mapping between the ID in banners and the ID in the reference table
  • Once the tables are merged I want to return the description from the reference table, which is the text representation of the ID.

I've been fiddling with this for a while and have hit a brick wall. Below is the code I am using (in LinqPad):

var results = (from b in Banners
         where b.BannerCode == "1234"
         from a in b.VesselBoatAreaY.Split (',').AsEnumerable()
         join r in References on a equals r.ReferenceCode
         where r.Context == "TestContext" 
         select r.Description).ToList();

I have confirmed that the first part of the query works, ie that banner code exists and returns 4 separate values. When I run the query as a whole however I get the following:

NotSupportedException Comparison operators not supported for type 'System.String[]'.

I have also tried the following:

var results = (from b in Banners
               where b.BannerCode == "1234"
               from a in b.VesselBoatAreaY.Split (',').AsEnumerable()
               from r in References
               where r.Context == "TestContext" &&
               a.Contains(r.ReferenceCode)
               select r.Description).ToList();

When I run this I get the following:

ArgumentException
The argument 'value' was the wrong type. Expected 'System.String'. Actual 'System.String[]'.

Any help appreciated!

Thanks for everyones help. I've solved the problem and it was actually very easy. As the table I am reading from is quite small I can apply AsEnumerable to the Banners table and it works fine. I realise this means it will get processed in memory, so it's not good for bigger tables, but its fine for what I need.

For reference the code is now:

var results = (from b in Banners.AsEnumerable()
               where b.BannerCode == "1234"
               from a in b.VesselBoatAreaY.Split (',')
               from r in References.AsEnumerable()
               where r.Context == "TestContext" &&
               a.Contains(r.ReferenceCode)
               select r.Description).ToList();

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