简体   繁体   English

实体框架-如何在查询中从字符串转换为整数

[英]Entity Framework - How to convert from String to Integer in query

I have a legacy database with the same ID stored with multiple representations (string and integer). 我有一个具有相同ID的旧数据库,其中存储了多个表示形式(字符串和整数)。 I need my query to join based on the key. 我需要基于该键的查询才能加入。

I know about SqlFunctions.StringConvert , but it doesn't work for my case, because the ID has 0-prefixes and the canonical representation of the number does not have string equivalence to other representations. 我知道SqlFunctions.StringConvert ,但是它不适用于我的情况,因为ID具有0前缀,并且数字的规范表示形式与其他表示形式没有字符串等效性。

How can I convert my numeric string value into an integer from within my query? 如何在查询中将数字字符串值转换为整数?

Not sure if that what you are looking for but since your numeric string may have characters in it you can extract just the numbers from string 不确定您要查找的是什么,但是由于数字字符串中可能包含字符,因此您只能从字符串中提取数字

var getNumbers =Convert.ToInt32 (from t in stringToQuery
                   where char.IsDigit(t)
                   select t).ToArray().ToString());

May be you should try something like this: 也许您应该尝试这样的事情:

    //example
    List<string> texts = new List<string>();
    List<int> integers = new List<int>();
    for (int j = 1; j <= 10; j++)
    {
        text.Add("00" + j.ToString());
        integers.Add(j);
    }
    var a = from t in texts
            join i in integers on Convert.ToInt32(t) equals i
            select t;

Can't you just use TrimStart? 您不能只使用TrimStart吗?

id.TrimStart('0'); id.TrimStart('0');

(edit) Actually LINQ to Entities doesn't like that so you need to try this instead to strip the leading zeros before the comparison: (编辑)实际上LINQ to Entities不喜欢这样,因此您需要尝试一下,而不是在比较之前去除前导零:

user trimstart in entity framework query 实体框架查询中的用户trimstart

I would create a class to store your representation. 我将创建一个类来存储您的表示形式。

public sealed class CanonicalInt: IEquatable<int>, IEquatable<string>
{
    private int _number;
    private string _canonical
    {
        get
        {
            return ""; //logic to turn int into format
        }
        set
        {
            _number = 0; ////logic to turn string into format
        }
    }

    public CanonicalInt(int number)
    {
        _number = number;
    }

    public CanonicalInt(string number)
    {
        _canonical = number;
    }

    public bool Equals(int other)
    {
        return _number.Equals(other);
    }

    public bool Equals(string other)
    {
        if(other == null)
            return false;

        return _canonical.Equals(other);
    }

    public static implicit operator int(CanonicalInt canonicalInt) 
    {
        return canonicalInt._number;
    }

    public static implicit operator string(CanonicalInt canonicalInt) 
    {
        return canonicalInt._canonical;
    }
}

Usage: 用法:

var number = new CanonicalInt(23);
var result = number == 23; // True

if your string always ends with canonical number them may be something like a combination of patindex,datalength and stringconvert ? 如果您的字符串始终以规范数结尾,则它们可能类似于patindex,datalength和stringconvert的组合? (please replace simulated SqlFunctions with real, it should run in 2entities context on tables then): (请用真实的替换模拟的SqlFunction,它应该在表的2entities上下文中运行):

string [] Strings = new string [] {"0015","0-00152","00-0012"};
int[] Integers = new int[] { 15,12};

var MixedResult = Strings.Where(s => Integers.Any(i => (PatIndex(StringConvert(i),s) + DataLength(StringConvert(i))) == DataLength(s))).ToList();

these are just simulated SqlFunctions: 这些只是模拟的SqlFunction:

private string  StringConvert(int x) 
{
    return x.ToString();
}

private int PatIndex(string pattern,string target)
{
      return target.IndexOf(pattern);
}

private int DataLength(string x) 
{
    return x.Length;
}

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

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